Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Me.Invoke in VB.NET doesn't actually "Invoke" - threads stall on Invoke statement

Tags:

I've got the following code:

Public Delegate Sub SetStatusBarTextDelegate(ByVal StatusText As String)
Private Sub SetStatusBarText(ByVal StatusText As String)
    If Me.InvokeRequired Then
        Me.Invoke(New SetStatusBarTextDelegate(AddressOf SetStatusBarText), StatusText)
    Else
        Me.labelScanningProgress.Text = StatusText
    End If
End Sub

The problem is that, when I call the "SetStatusBarText" sub from another thread, InvokeRequired is True (as it should be), but then my threads stall on the Me.Invoke statement - pausing execution shows them all just sitting there, not actually invoking anything.

Any thoughts about why the threads seem to be afraid of the Invoke?

like image 227
SqlRyan Avatar asked Mar 16 '10 21:03

SqlRyan


1 Answers

The Invoke method puts a message in the message queue to perform the method call in the main thread. This means that you need a main thread with a message pump. If the main thread is busy, or if you are doing this in an application without a message pump (e.g. a console application), the message will just remain in the queue.

like image 71
Guffa Avatar answered Sep 28 '22 18:09

Guffa