I have a problem with the busyindicator control of silverlight.
I have a datagrid (datagrid1) with its source set to a wcf service (client).
I would like to use the busyindicator control (bi) of silvelright toolkit when the datagrid loads itself.
But I have an “Invalid cross thread access” when I use “ThreadPool”.
Sub LoadGrid()
Dim caisse As Integer = ddl_caisse.SelectedValue
Dim env As Integer = ddl_env.SelectedValue
bi.IsBusy = True
ThreadPool.QueueUserWorkItem(Sub(state)
AddHandler client.Get_PosteSPTCompleted, AddressOf client_Get_PosteSPTCompleted
client.Get_PosteSPTAsync(caisse, env)
Dispatcher.BeginInvoke(Sub()
bi.IsBusy = False
End Sub)
End Sub)
End Sub
Private Sub client_Get_PosteSPTCompleted(sender As Object, e As ServiceReference1.Get_PosteSPTCompletedEventArgs)
DataGrid1.ItemsSource = e.Result ' Here, Invalid cross thread access
End Sub
I know that the datagrid control doesn’t exist in the “new thread”, but how have to I make to avoid this error?
Thank you.
William
ThreadPool?Using a
ThreadPoolwould be a good idea if your service was synchronous, but your WCF service is asynchronous: it won’t block your UI thread after being called usingGet_PosteSPTAsync.IsBusyproperty. You’re first setting it totrue, then you starts an operation asynchronously, then you set it tofalseimmediately. You should set it to false in theCompletedhandler.client_Get_PosteSPTCompletedhandler won’t be executed in the same thread as the UI thread (even if you don’t use theThreadPool). That’s why you’ll have to use the dispatcher here so force using the UI thread.Your
DataGrid1.ItemsSource = e.Resultmust be modified to:(note sure about the VB.Net syntax, but that’s the idea)
clientobject lifetime, but you’re adding a new handler to theGet_PosteSPTCompletedeach time you callLoadGrid. Maybe you could think of detaching handlers after use.