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

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top