Events in a .NET thread

T

Tom

First of all, is it 'legal' (i.e. thread-safe) to pass events back from
a worker thread to the main thread? I.E. Can one put PUBLIC EVENT XYZ
in their worker thread and then raise that event for processing back in
the main thread? I believe this is OK, but I want to check first...

Secondly, if this is 'legal', what happens in the following scenario:
Lets say that in my main (Win Form) program/thread I spawn two worker
threads. Thread one does some work then raises the 'ImAlmostDone' event
back to my main thread. Now, at about the same time, my second thread
also raises a 'ImAlmostDone' event back. In this case, do both events
get processed in the main thread? And do they get processed in the
proper order - i.e. thread 1's event, then thread 2. Or do I need to do
any thread locking before I process the event (i.e. to make sure the
main thread finishes the event before the next thread raises the same
event)

Before I start passing events back, I want to make sure this is going
to work and I am not going to screw things up.

Thanks in advance.

Tom
 
S

Some Guy

A thread that raises an event handles the event. So in this case, your
threads will handle the events and not your main thread. If you want the
form's thread to handle the events for you, you should use Me.Invoke or
Me.BeginInvoke to call a delegate.
 

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