Thread safety and events

  • Thread starter Thread starter Emrys
  • Start date Start date
E

Emrys

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.
 
Emrys said:
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/threads/lockchoice.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 said:
See http://www.pobox.com/~skeet/csharp/threads/lockchoice.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
 
Jesse McGrew said:
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/threads/volatility.shtml
 
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 :)
 

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

Back
Top