471,317 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

Thread safety and events

Hi.

I have two threads and an event. The first thread registers a callback
with the event. That callback plays with variables that only the first
thread should touch. The second thread triggers the event. Is this
thread-safe? If not, should I be doing thread synchronization
(locking, etc.) with Windows Forms callbacks?

If this has an obvious answer, feel free to yell and/or point me to the
appropriate web page.

Dec 24 '05 #1
5 1232
Emrys <em****@gmail.com> wrote:
I have two threads and an event. The first thread registers a callback
with the event. That callback plays with variables that only the first
thread should touch. The second thread triggers the event. Is this
thread-safe? If not, should I be doing thread synchronization
(locking, etc.) with Windows Forms callbacks?

If this has an obvious answer, feel free to yell and/or point me to the
appropriate web page.


See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml for my
advice on making events thread-safe so you can subscribe/unsubscribe
from other threads.

However, if your event handlers need to be run in a different thread to
the thread which triggers the event, I'd suggset making the handlers
themselves do the appropriate thing (eg using Control.BeginInvoke).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 24 '05 #2
Jon wrote:
See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml for my
advice on making events thread-safe so you can subscribe/unsubscribe
from other threads.


At the end of the article, you mention how to write an event raising
method (OnSomeEvent) and give a couple examples of how not to do it.
What do you think about the following implementation?

protected virtual OnSomeEvent(EventArgs e)
{
SomeEventHandler handler;
handler = someEvent;
if (handler != null)
{
handler (this, e);
}
}

This is the same as your recommended code, but without the lock around
"handler = someEvent". Is the lock really necessary when you're only
reading a single object reference?

Jesse

Dec 24 '05 #3
Jesse McGrew <jm*****@gmail.com> wrote:
Jon wrote:
See http://www.pobox.com/~skeet/csharp/t...ckchoice.shtml for my
advice on making events thread-safe so you can subscribe/unsubscribe
from other threads.


At the end of the article, you mention how to write an event raising
method (OnSomeEvent) and give a couple examples of how not to do it.
What do you think about the following implementation?

protected virtual OnSomeEvent(EventArgs e)
{
SomeEventHandler handler;
handler = someEvent;
if (handler != null)
{
handler (this, e);
}
}

This is the same as your recommended code, but without the lock around
"handler = someEvent". Is the lock really necessary when you're only
reading a single object reference?


Yes, unless the variable is marked as volatile. Otherwise you might see
"old" values.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 24 '05 #4
I'm using System.Threading.Timer with parameters to call it imediately
and only once, because you can pass an object to the callback function
so I don't have to use global variables, but that probably isn't
answer to your problem :)

Dec 24 '05 #5
Thank you.

Dec 24 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steve McLellan | last post: by
4 posts views Thread by Jonathan Burd | last post: by
5 posts views Thread by Fred West | last post: by
5 posts views Thread by jzlondon | last post: by
4 posts views Thread by Warren Sirota | last post: by
6 posts views Thread by fniles | last post: by
3 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.