How can I get my event handler to respond to an event raised in ANOTHER thread?

L

Lisa B.

It appears that this was possible in Visual Studio 2003, but no longer works
in 2005.

Let me state the problem more fully.

In my main thread, I have a class with an event handler. In the code for
this class, I create an instance of my worker class, and call one of the
methods of this worker class using a separate thread. The worker method
raises the event which my event handler is listening for.

Well, as it turns out, in VS2005, the event handler code is executed IN THE
SAME THREAD as the background method which raised the event. This really
sucks, because any code to update the GUI will fail.

Has anyone else noticed this? And is there some kind of workaround? I was
really hoping to have some way for an event raised in my background thread
to be able to trigger an event handler which executes in the main thread.

Thanks if you can help!

Lisa B.
 
A

AlexS

I believe same was true in previous versions of .Net. Most probably you just
did miss that earlier or did not get into troubles like exceptions.

You need to check if InvokeRequired and then BeginInvoke in your handler -
check both methods in MSDN docs or VS help. This is standard pattern for
handling events raised in another thread.

HTH
Alex
 
G

Guest

Well, as it turns out, in VS2005, the event handler code is executed
IN THE SAME THREAD as the background method which raised the event.
This really sucks, because any code to update the GUI will fail.

Windows GUIs are single threaded so GUI updates must be done on the GUI
thread (primary thread).

VS.NET 2003 allowed you to update the GUI from any thread, but this
often caused strange errors with the application. As a result, VS.NET
2005 now has additional checks to prevent this sort of thing
Has anyone else noticed this? And is there some kind of workaround?
I was really hoping to have some way for an event raised in my
background thread to be able to trigger an event handler which
executes in the main thread.

Here is the pattern

http://www.dotnetjunkies.com/WebLog/bbenbachir/archive/2006/04/29/137586
..aspx

Bascially:

If Form.InvokeREquired then
Form.BeginInvoke(DelegateGoesHere) 'Call self through marshaled
delegate
Else
'Process as normal
End If
 
R

rowe_newsgroups

It appears that this was possible in Visual Studio 2003, but no longer works
in 2005.

Let me state the problem more fully.

In my main thread, I have a class with an event handler. In the code for
this class, I create an instance of my worker class, and call one of the
methods of this worker class using a separate thread. The worker method
raises the event which my event handler is listening for.

Well, as it turns out, in VS2005, the event handler code is executed IN THE
SAME THREAD as the background method which raised the event. This really
sucks, because any code to update the GUI will fail.

Has anyone else noticed this? And is there some kind of workaround? I was
really hoping to have some way for an event raised in my background thread
to be able to trigger an event handler which executes in the main thread.

Thanks if you can help!

Lisa B.

Could you please stop requesting that your messages not be archived?

One of the primary benefits of this newsgroup is that people can
search the archives and find the answers to their question before
posting. If your message is removed from the archives it will make it
harder for others to find there answers in the archives.

Thanks,

Seth Rowe
 

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