Raising and event

G

Guest

Hi,

I have an app with the main thread and one additional thread.

In the main thread I defined an event handler MyHandler, the function for
raisin this event OnMyEvent and the function for processing this event MyFunc.

I want to raise this event from the second thread. Is it thread safe to call
the method OnMyEvent from this second thread?

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?

public event MyEventHandler MyEvent;
MyEvent += new MyEventHandler (MyFunc);

protected virtual void OnMyEvent( EventsArgs e)
{
MyEventHandler handler = MyEvent;
if (handler != null)
{
handler(this,e);
}
}

void MyFunc( object sender, EventArgs e)
{
....
}

Thanks,

Lubomir
 
P

Peter Duniho

[...]
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
 
J

Jon Skeet [C# MVP]

Lubomir said:
I have an app with the main thread and one additional thread.

In the main thread I defined an event handler MyHandler, the function for
raisin this event OnMyEvent and the function for processing this event MyFunc.

I want to raise this event from the second thread. Is it thread safe to call
the method OnMyEvent from this second thread?

Not the way you're doing it at the moment. See
http://pobox.com/~skeet/csharp/threads/lockchoice.shtml for a thread-
safe event pattern.
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?

No, there's no messages being passed here - events are just a way of
combining/removing delegates, effectively.

See http://pobox.com/~skeet/csharp/events.html for more information.
 

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

Similar Threads

How can these lines be rewritten 15
Handling Events in C#.NET 5
Concurrency and delegates 13
Asynchronous event 2
When is an event null? 22
Delegates VERSUS Events 6
Events and Thread Safety 5
Use of event handling 6

Top