On Tue, 27 Mar 2007 19:07:24 -0700, Lubomir
<Lu*****@discus sions.microsoft .comwrote:
[...]
I want to raise this event from the second thread. Is it thread safe to
call the method OnMyEvent from this second thread?
That depends on what you're actually doing in the event handler.
There's nothing fundamentally wrong about raising an event from a
secondary thread. However, a) AFAIK events are just like any other
variable with respect to thread-safeness, which means that if there's a
chance one thread may be modifying the event while another thread attempts
to access it, you need to provide synchronization for that, and b) you do
need to be aware of the restriction against calling Control methods from a
thread other than the one in which the Control was created (use Invoke or
BeginInvoke to get around that).
The main thing to remember is that the event handler will be executed on
the thread where the event was raised, even if it belongs to a Form or
Control created in another thread. All the general rules about
cross-thread execution apply, but as far as I know there's nothing
particularly special about the situation.
In Win32 with C++ I waas just sending WM messages between threads. Does
work OnMyEvent overloaded method the same way, so I could call it from
different threads?
I'm not sure what you mean by "just sending WM messages between threads",
but note that if you were actually calling SendMessage() from one thread
using a window handle from a window created on a different thread, that
was actually against the rules and not reliable. In fact, that
restriction is the reason behind the .NET restriction against calling
Control methods from a thread other than the one on which the Control was
created.
Pete