473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Safely Raising Events in Multithreaded app.

This questions is in reference to the article on Using Events in the C#
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).

Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes sense.
My question is is this: if we really want this to be thread safe why doesn't
this copy have to be within a locked block. For example, wouldn't this be a
better solution:

lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;
}

if (temp != null)
{
temp(this, new System.EventArgs());
}

Or is there something I'm missing here?

Thanks,

Mar 2 '07 #1
3 3088
Agreed, your solution stops one thread copying the TestEvent object to
temp whilst another thread may be updating TestEvent.

On 2 Mar, 16:40, smkramer072fdasfasdf
<smkramer072fdasfa...@discussions.microsoft.comwro te:
This questions is in reference to the article on Using Events in the C#
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).

Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes sense.
My question is is this: if we really want this to be thread safe why doesn't
this copy have to be within a locked block. For example, wouldn't this be a
better solution:

lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;

}

if (temp != null)
{
temp(this, new System.EventArgs());

}

Or is there something I'm missing here?

Thanks,

Mar 2 '07 #2
lock(this) wouldn't help anything if another thread wanted to add
subscription to the event.
It does not protect that the event would get changed (lock(this) is not
freeze every write in this class, and generally should not be used).

The meaning of taking a temp reference is that if the last subscriber of the
event unsubscribes, the TestEvent would be null and the TestEvent() call
would fail for null reference. Instead, you take the reference to a temp
var. If the var was not null, it won't be null thereafter, even if the last
subscriber unsubscribes. The temp var is still not null.
Hence the temp() call will not crash even if there are no subscribers left.
It will at most be a nop.
"smkramer072fdasfasdf" <sm******************@discussions.microsoft.comh a
scritto nel messaggio
news:73**********************************@microsof t.com...
This questions is in reference to the article on Using Events in the C#
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).

Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes
sense.
My question is is this: if we really want this to be thread safe why
doesn't
this copy have to be within a locked block. For example, wouldn't this be
a
better solution:

lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;
}

if (temp != null)
{
temp(this, new System.EventArgs());
}

Or is there something I'm missing here?

Thanks,

Mar 2 '07 #3
Very true, lock blocks access to a block of code in an object from
multiple threads. Putting a lock around the copy of the thread only
stops more than one thread copying the event at the same time if they
are invoking the method in the same object (i.e. all have a reference
to the object that contains the method).

On 2 Mar, 17:13, "Laura T." <L...@NOWHERE.COMwrote:
lock(this) wouldn't help anything if another thread wanted to add
subscription to the event.
It does not protect that the event would get changed (lock(this) is not
freeze every write in this class, and generally should not be used).

The meaning of taking a temp reference is that if the last subscriber of the
event unsubscribes, the TestEvent would be null and the TestEvent() call
would fail for null reference. Instead, you take the reference to a temp
var. If the var was not null, it won't be null thereafter, even if the last
subscriber unsubscribes. The temp var is still not null.
Hence the temp() call will not crash even if there are no subscribers left.
It will at most be a nop.

"smkramer072fdasfasdf" <smkramer072fdasfa...@discussions.microsoft.comh a
scritto nel messaggionews:73********************************** @microsoft.com...
This questions is in reference to the article on Using Events in the C#
Programmers Reference
(http://msdn2.microsoft.com/en-us/library/ms173168.aspx).
Under the Rasing Events section it says in order to avoid a race condition
copy the event to a temporary variable before invoking it. That makes
sense.
My question is is this: if we really want this to be thread safe why
doesn't
this copy have to be within a locked block. For example, wouldn't this be
a
better solution:
lock (this)
{
// Safely invoke an event:
TestEventDelegate temp = TestEvent;
}
if (temp != null)
{
temp(this, new System.EventArgs());
}
Or is there something I'm missing here?
Thanks,

Mar 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
2
by: Mark | last post by:
I'm trying to raise and handle an event. I'm using an example from the VS.NET help files (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht...
0
by: Greg Park | last post by:
I have many user controls loading into a web page using Page.LoadControl However, I'm unable to figure out how to raise an event when a button is click or a check box is checked. I can have...
0
by: Joe Campbell | last post by:
I am encountering a problem raising WMI events from an asp.net application. The error received (as captured in the event log) is as follows: System.Runtime.InteropServices.COMException...
4
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
0
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.