473,490 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1284
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
1980
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
1974
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
6627
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
1675
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
3387
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
2542
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
3123
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
1982
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
1527
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
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.