Raising events cross threads

W

Wavemaker

I have a component class that has a set of events. The class
uses a worker thread to do some processing. When it is
finished with the processing, it raises the appropriate
events.

Problem, since these events are being raised in the worker
thread, the clients connected to the events are receiving
notification outside of the thread in which they belong.

Is there a way to raise events in such as way as to
guarantee that the notification is sent to the proper
thread?
 
R

Richard Blewett

when you say componentm as you talking about a visual component or just a
class you have written (unfortunately its a overloaded term).

If visual you can call Control.BeginInvoke that will marshal the call to the
UI thread.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
 
W

Wavemaker

Richard Blewett said:
when you say componentm as you talking about a
visual component or just a class you have written
(unfortunately its a overloaded term).

The class derives from System.ComponentModel.Component.
If visual you can call Control.BeginInvoke that will
marshal the call to the UI thread.

Unfortunately, the Component class doesn't have the
BeginInvoke method, which makes sense, I suppose, but I
would sure find it handy in the situation I'm in. :)

Doing some research on this since posting, it seems the best
course of action for me is to document thoroughly that the
events raised by my component are in their own thread so
that listeners can respond appropriately. For example, a
Form would test the InvokeRequired property and use the
BeginInvoke method if necessary.
 

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