M
minimega
Hello to all NG.
Using events in C# I've the following code:
MyObject myObject = new MyObject();
myObject.Click += new MyObject.ClickHandler(ClickFunction);
so, everytime myObject class raises a new click event, I can process
that event in my other class in ClickFunction function.
But if I want to remove the handling of that event I must use the -=
operator like:
myObject.Click -= new MyObject.ClickHandler(ClickFunction);
Is this right? Or if I .Dispose() the class the event handler is
disposed automatically?
However, why can't I use the following sintax to remove the event
handling?
myObject.Click += null; or myObject.Click -= null;
Why must I create a new istance of MyObject.ClickHandler(ClickFunction)
if I have only to remove the event handling from the class?
If I use null instead "new MyObject.ClickHandler(ClickFunction)" it
seems that the event handling doesn't stop and the event is always
fired. If in the ClickFunction there are references to objects that are
just disposed (for example after the class was unloaded) I get a
NullException error. But how can a event handler callback a function
(ClickFunction) in a unloaded instance of a class? If there are active
references to events handler and I .Dispose() the class, that class is
really disposed and all recferences set to null, or the class keep
alive until I remove _manually_ the event handling with myObject.Click
-= new MyObject.ClickHandler(ClickFunction)?
To better understand:
I've the myStatistics class that raises Updated() event every minute,
sending information about last minute data to all the classes who have
"attached" that event. I create a new istance of myForm class that add
a event handling of myStatistics class using the += operator, to get
updated data every minute. If I .Dispose() myForm class without
manually remove the event handling with -= operator, the myStatistics
class raises always the Updated() event to myForm class (just
..Disposed)?
Can anybody explain me this behavior?
Thanks,
Massimo
Using events in C# I've the following code:
MyObject myObject = new MyObject();
myObject.Click += new MyObject.ClickHandler(ClickFunction);
so, everytime myObject class raises a new click event, I can process
that event in my other class in ClickFunction function.
But if I want to remove the handling of that event I must use the -=
operator like:
myObject.Click -= new MyObject.ClickHandler(ClickFunction);
Is this right? Or if I .Dispose() the class the event handler is
disposed automatically?
However, why can't I use the following sintax to remove the event
handling?
myObject.Click += null; or myObject.Click -= null;
Why must I create a new istance of MyObject.ClickHandler(ClickFunction)
if I have only to remove the event handling from the class?
If I use null instead "new MyObject.ClickHandler(ClickFunction)" it
seems that the event handling doesn't stop and the event is always
fired. If in the ClickFunction there are references to objects that are
just disposed (for example after the class was unloaded) I get a
NullException error. But how can a event handler callback a function
(ClickFunction) in a unloaded instance of a class? If there are active
references to events handler and I .Dispose() the class, that class is
really disposed and all recferences set to null, or the class keep
alive until I remove _manually_ the event handling with myObject.Click
-= new MyObject.ClickHandler(ClickFunction)?
To better understand:
I've the myStatistics class that raises Updated() event every minute,
sending information about last minute data to all the classes who have
"attached" that event. I create a new istance of myForm class that add
a event handling of myStatistics class using the += operator, to get
updated data every minute. If I .Dispose() myForm class without
manually remove the event handling with -= operator, the myStatistics
class raises always the Updated() event to myForm class (just
..Disposed)?
Can anybody explain me this behavior?
Thanks,
Massimo