Getting a task queued on a secondary thread to run on the main thread

I have a msg queue that runs on it's own thread that receives msg commands. Does anyone know of a way to launch a task or raise an event on the main thread from the queue thread?

My goal is to receive task on the queue and execute them one at a time on the main thread.

Thank you for you time and kindness.

[386 byte] By [Elliott_Ward] at [2007-12-24]
# 1
Hi Elliott,

By far the easiest way is to use Control.Invoke (or BeginInvoke). But that requires a reference to an initialized form instance. Do you have one that you can pass to the thread's constructor?

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

I could build it that way if required, but the requirements give serveral commands that require reporting back to the GUI, that will involve several controls, and doesn't that mean each control would require an invoke and so on?

Elliott_Ward at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
You

can just use the form instance to call Invoke, it will ensure that the

delegate runs on the main GUI thread. You'll need different delegates

for each distinct callback.

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

I would set this up using events, doing something like this:

Public Delegate Sub ActionEventHandler()

Public Class MessageQueueProcessor

Public Event Action1 As ActionEventHandler
Public Event Action2 As ActionEventHandler
'... Add additional events as necessary

Public Sub StartProcessingEvents()
'...Add code here to interface with MSMQ
End Sub
End
Class

you can then singal events from the message queue thread back to the UI thread by firing events.

You can then hookup the events really easily using a withevent variable....

Class MainGUIForm
Private WithEvents queProcessor As MessageQueueProcessor

Private Sub HandleAction1() Handles queProcessor.Action1

'determine if we are not on the form's main thread, and if so, marshal
'the call over to that main forms thread.
If (InvokeRequired) Then
'The call to invoke here causes the MessageQueueProcessor thread
'to block untill the code in the "else" block below completes on the UI thread.
'If you do not wish to block the messagequeueprocess, change this to BeginInvoke.
'However, be warned that if you do this it will then become possible for your form
'to process messages out of order.

Invoke(CType(AddressOf HandleAction1, ActionEventHandler))
Else
'Put code to perform action1 here....
End If
End Sub

'Repeat the pattern above for the remaining actions....
End Class

To have the que processor beging processing messages on a seperate thread, you can then put the following code inside your form load event:

new Thread(Addressof queProcessor.StartProcessingEvents()).Start()

-Scott Wisniewski

ScottWisniewski-MSFT at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
Hi Scott,

Do me a favor and read the last paragraph in Joe Morel's blog article.

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...