How do I fire an event to the main thread from the catch block of another thread

  • Thread starter Thread starter Sweta
  • Start date Start date
S

Sweta

How do I post a message to the main thread from another
thread in the catch block? I am trying to do this:

try
{
//generate an exception from another thread
}
catch()
{
//fire the event to the main thread
}


I get this error:
"An unhandled exception of
type 'System.InvalidOperationException' occurred in
system.dll

Additional information: The action being performed on this
control is being called from the wrong thread. You must
marshal to the correct thread using Control.Invoke or
Control.BeginInvoke to perform this action."

Thanks,
Sweta
 
Sweta,

You should define a method on a control that resides in your main
thread. This method will fire the event. Once you do that, you create a
delegate with the same signature of that method. Finally, in your worker
thread, call the Invoke method on the control that has the method, and pass
the delegate (pointing to the control method), along with any parameters
that are needed.

Hope this helps.
 
Back
Top