472,958 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1270
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
4
by: Steve McLellan | last post by:
Hi, I've just discovered that accessing Image.Height (and probably other properties) in a multi-threaded environment can cause access problems (as a test, you can spawn a hundred threads and get...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
5
by: Fred West | last post by:
I have an object with events that are subscribed to, from different threads. My code currently looks like this: private event MyEventHandler myEvent = null; public event MyEventHandler MyEvent...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
7
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class...
3
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've yet another question. I have an assembly that provides the interface for a scanner. The scanner has a method on it called Read(). The prototype is as follows... ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.