sloan
10/29/2008 6:14:00 PM
// private void OnRootFolderEventArgs(RootFolderEventArgs arg)//
Good point, I (now) see the unneeded (double use of ~this~) in my code.
Aka, the above method doesn't need the first argument of sender.
//> EventHandler<RootFolderEventArgs> handler =
> SomethingHappenedWithARootFolderEvent;//
//Actually, you don't need to declare a delegate type anymore.//
Correct. This was some leftover 1.1 code that got upconverted to 2.0 (in my
original).
.......
I think the guy is on the right path now.
"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.ujsrit0c8jd0ej@petes-computer.local...
> On Wed, 29 Oct 2008 10:00:49 -0700, David
> <david.colliver.NEWS@revilloc.removethis.com> wrote:
>
>> Thanks, so in my own example, it is the bluetooth.cs that needs to raise
>> the
>> event... and it is the form1.cs that needs to handle it.
>>
>> Where would the delegate go?
>
> Actually, you don't need to declare a delegate type anymore. You do need
> to still declare your EventArgs sub-class, and that should go in the same
> assembly with the Bluetooth class. For the delegate type, you can use the
> generic EventHandler<T> class, where the type parameter is your own
> EventArgs sub-class.
>
>> your MyClass I am assuming is the same as my form1.cs
>
> In his code sample, it appears to me that MyClass is equivalent to your
> Bluetooth class. That is, it's the class that raises the event, not the
> one that consumes it.
>>
>> I am also assuming that your SomeNormalCode() is what should be in my
>> bluetooth.cs
>
> For his example, I believe that is correct.
>
> One note: the posted code for raising an event, both yours and sloan's, is
> appropriate for single-threaded scenarios, but not multi-threaded.
> Sloan's is better in that there is a dedicated event-raising method, but
> even his has a threading bug in it. A correct method would look like
> this:
>
> private void OnRootFolderEventArgs(RootFolderEventArgs arg)
> {
> EventHandler<RootFolderEventArgs> handler =
> SomethingHappenedWithARootFolderEvent;
>
> if (handler != null)
> {
> handler(this, arg);
> }
> }
>
> By copying the delegate value before checking it for null, you ensure that
> the value can't change from non-null to null between the time you look at
> it and the time you try to use it.
>
> Note also that I've removed the "sender" argument to the method. This
> should be an instance method, and the sender should always be "this", so
> there's no need to pass it as an argument.
>
> One other note: in this example, the EventArgs sub-class itself is being
> passed, but it would actually be very reasonable to have the caller simply
> pass whatever data goes into the sub-class and then have this method
> actually create the sub-class instance. This is especially true if the
> sub-class only has a small number of data elements in it (for example,
> there's just a single string).
>
> Pete