views:

173

answers:

3

Hi,

when a button is clicked i start a seperate thead which is population a grid and does something with a webbrowser-control. But when the button is click the new thread seems not to be seperate, cause the UI is freezing until the new thread is done.

Here the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    FindCustomerLocation()
    e.Handled = True
End Sub


Private Sub FindCustomerLocation()
    Dim Findcontractor_Thread As New Thread(AddressOf FindContractor_ThreadExecute)
    Findcontractor_Thread.Priority = ThreadPriority.AboveNormal
    Findcontractor_Thread.Start()
End Sub


Delegate Sub FindContractorDelegate(ByVal igGrid As Infragistics.Windows.DataPresenter.XamDataGrid, ByVal webbrowser As Controls.WebBrowser)


Private Sub FindContractor_ThreadExecute()
    Dim threadControls(1) As Object
    threadControls(0) = Me.XamDataGrid1
    threadControls(1) = Me.WebBrowserMap

    Dim m As FindContractorDelegate = AddressOf FindContractor_WorkingThread
    Me.Dispatcher.BeginInvoke(m, threadControls)
End Sub


Private Sub FindContractor_WorkingThread()

    Mouse.OverrideCursor = Cursors.Wait

'Do something...

    Mouse.OverrideCursor = Nothing
End Sub

What I'm doing wrong?

Thanks, Neils

+2  A: 

You propabldy working with the winforms controls in the "'do something". The controls can be mainupulated only in UI thread.

So, the "BeginInvoke" call the target in ths UI thread. So you create te paralel thread, but the whole think is doing in UI thread again.

TcKs
A: 

Your new thread has a higher priority, so will cause the UI to freeze whatever it does (as long as it is partly intensive)

ck
+2  A: 

Use Dispatcher.CurrentDispatcher.BeginInvoke to solve this problem.

The issue occurs because the Dispatcher instance invokes on the GUI thread but Dispatcher.CurrentDispatcher will create a new Dispatcher instance for the current executing thread if one does not exist.

This is similar in concept to how Windows will create message queues for new threads that are created that themselves create a winform.

Mike J