Re: Safely Raising Events in Multithreaded app.



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...@xxxxxxxxxxx> wrote:
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...@xxxxxxxxxxxxxxxxxxxxxxxxx> ha
scritto nel messaggionews:7338697F-71F9-4055-A225-9E854FAD9D66@xxxxxxxxxxxxxxxx

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,


.



Relevant Pages

  • Re: ACCESS97, try to relink reference to another MDB
    ... when you move your application to the second machine, the reference to ... Dim ref As Reference ... Set ref = Application.References ... Dim temp$, resp ...
    (comp.databases.ms-access)
  • Re: Delegates and Events confusion
    ... I figured that I needed to unsubscribe in some way but since I ... wasn't storing an internal reference to the subscriber. ... In ClassC I hook into the ClassA ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Events unsubscribing and resource leaks?
    ... reference like any other reference. ... Garbage collection is not deterministic. ... If you know when the subscriber is "no longer needed" you have to be ... subscriberMethod, EventInfo provider, Type eventArgumentType, Type ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Events unsubscribing and resource leaks?
    ... unsubscribe then dispose the instance in order to allow garbage collection ... important to unsubscribe from events before you dispose of a subscriber ... reference like any other reference. ... subscriberMethod, EventInfo provider, Type eventArgumentType, Type ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SYS$QIOW - Performance Issue
    ... you would see a reference count of 2. ... $OPEN /READ/WRITE TEMP MBAnnn: ... stuck in the WRITE statement and you might need to kill the process. ... for the reading process to read from mailbox). ...
    (comp.os.vms)