Neils
Neils

Reputation:

New Thread is still blocking UI-Thread

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

Upvotes: 1

Views: 1583

Answers (4)

Pakk
Pakk

Reputation: 1339

Dim Findcontractor_Thread As New Thread(AddressOf FindContractor_ThreadExecute)
Findcontractor_Thread.Priority = ThreadPriority.AboveNormal
Findcontractor_Thread.Start()

for threads calls with parameters

trd_copy.ParameterizedStart(src)

Delegate Sub nameofDelegate(s As Integer)
Sub nameofDelegate+NameofSub(ByVal s As Integer)
If Form1.ProgressBar1.InvokeRequired Then
    Dim d As New nameofDelegate (AddressOf nameofDelegate+NameofSub)
    NameOfYourForm.Invoke(d, New Object() {s})
Else
    If s = 1 Then
        NameOfYourForm.ProgressBar1.Refresh() ** Or other Gui Functions
    Else

    End If
End If

End Sub

For Thread calls without parametrs

trd_copy.Start()

Delegate Sub nameofDelegate()
Sub nameofDelegate+NameofSub()
If Form1.ProgressBar1.InvokeRequired Then
    Dim d As New nameofDelegate (AddressOf nameofDelegate+NameofSub)
    NameOfYourForm.Invoke(d, New Object())
Else
   NameOfYourForm.ProgressBar1.Refresh() ** Or other Gui Functions
End If
End Sub

Keep in mind when you first Start a Thread and you are coding in a Model you MUST pass (me) into the initial thread because of VB having a concept of "Default Form Instances". For every Form in the application's namespace, there will be a default instance created in the My namespace under the Forms property.

and that is just adding an additional parameter like so

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



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


FindContractor_WorkingThread(threadControls,beginform)
End Sub


Delegate Sub FindContractor_WorkingThread(s As Integer,beginform as   *NameOfFormComingFrom*)
Sub FindContractor_WorkingThreadInvoke(ByVal s As Integer,beginform as *NameOfFormComingFrom*)
If beginform.mouse.InvokeRequired Then
    Dim d As New FindContractor_WorkingThread(AddressOf    FindContractor_WorkingThreadInvoke)
    beginform.Invoke(d, New Object() {s,beginform})
Else
     beginform.Mouse.OverrideCursor = Cursors.Wait

    'Do something...

    beginform.Mouse.OverrideCursor = Nothing
End If
End Sub

Upvotes: 1

Mike J
Mike J

Reputation: 3149

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.

Upvotes: 2

cjk
cjk

Reputation: 46425

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

Upvotes: -2

TcKs
TcKs

Reputation: 26632

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.

Upvotes: 4

Related Questions