views:

646

answers:

1

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?

+2  A: 

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.

Guffa
That's exactly what was going on - the "Me.Invoke" was waiting for the main thread to respond, but the main thread was still busy cramming stuff into the Threadpool as the first threads were finishing up. Thanks for your tip.
rwmnau
Thanks! I was waiting for the Invokes to finish but they were all frozen. I added a call to System.Windows.Forms.Application.DoEvents() and it works.
the_lotus