Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I don't know which thread to access for the Dowork process:

I have the following code in the DomainDataSource LoadedData

<pre lang="vb">If (DataGrid.ItemsSource IsNot Nothing) Then
            Dim count As Integer = DataGrid.ItemsSource.OfType(Of Object)().Count()
            lblCount_Stat.Content = "Count: " & count
        End If



I get an Invalid Thread Access when I try to access the datagrid as it is loading

<pre lang="vb">Private Sub BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker.DoWork
        Dim result As String

        result = Me.DataGrid.ItemsSource.OfType(Of Object)().Count()

        e.Result = result
    End Sub

Posted

It's because you're trying to access a UI element that was created on another thread. You have to use Invoke to do that.

http://www.silverlightshow.net/tips/How-to-invoke-method-in-the-UI-thread.aspx[^]
 
Share this answer
 
You probably need to use Dispatcher.BeginInvoke.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Feb-11 15:24pm    
Absolutely, my 5 (well, just Invoke would do, in most case as well as in this case).
--SA
Nish Nishant 8-Feb-11 15:27pm    
I agree, and thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900