How to unregister an event
-
I have a procject in which i am using a custom webclient. I have following event _WebClient.DocumentComplete which i can register and it works. Now if I want to unregister the first event handler and assign a new event handler I gives me error because I can't use
_WebClient.DocumentComplete = null;
Can some 1 tell me how to unregister the evento O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
Hi, you can undo someEvent+=new SomeEventHandler(myHandler); with the following magical line: someEvent-=new SomeEventHandler(myHandler); The magic is you create a new object to remove an old (but very similar) object ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
I have a procject in which i am using a custom webclient. I have following event _WebClient.DocumentComplete which i can register and it works. Now if I want to unregister the first event handler and assign a new event handler I gives me error because I can't use
_WebClient.DocumentComplete = null;
Can some 1 tell me how to unregister the evento O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°
-
Hi, you can undo someEvent+=new SomeEventHandler(myHandler); with the following magical line: someEvent-=new SomeEventHandler(myHandler); The magic is you create a new object to remove an old (but very similar) object ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday. :) How does this work if buggy code results in the same handler being registered twice?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Hi, you can undo someEvent+=new SomeEventHandler(myHandler); with the following magical line: someEvent-=new SomeEventHandler(myHandler); The magic is you create a new object to remove an old (but very similar) object ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
Hello Luc,
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I remember that Scott Dorman once pointed out that it is even possible without creating an new object (I think only in .Net>1.1) Here is the thread![^] P.S.: This was my Nr.:1000 post.
All the best, Martin
-
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday. :) How does this work if buggy code results in the same handler being registered twice?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
Dont know, havent seen it spec'd, so I can only suggest you try it. I can confirm if you register twice, you execute twice (that is a common bug, that harms performance and is not always apparent). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hello Luc,
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I remember that Scott Dorman once pointed out that it is even possible without creating an new object (I think only in .Net>1.1) Here is the thread![^] P.S.: This was my Nr.:1000 post.
All the best, Martin
Hi Martin, I did remember that thread; I havent tried it yet, I still tend to write code that runs also on 1.1
Martin# wrote:
This was my Nr.:1000 post
Aha, that explains a few things. Now take that holiday ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi Martin, I did remember that thread; I havent tried it yet, I still tend to write code that runs also on 1.1
Martin# wrote:
This was my Nr.:1000 post
Aha, that explains a few things. Now take that holiday ! :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday. :) How does this work if buggy code results in the same handler being registered twice?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
-
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday. :) How does this work if buggy code results in the same handler being registered twice?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
Hi Dan, I ran a little test:
private int clicks=0; private int handlers=0; Button btn=new Button(); public override void Run(int arg) { doClick(); addHandler(); doClick(); addHandler(); doClick(); removeHandler(); doClick(); removeHandler(); doClick(); removeHandler(); doClick(); } void doClick() { log("CLICK"); btn.PerformClick(); } void addHandler() { handlers++; log("ADD HANDLER "+handlers); btn.Click+=new EventHandler(btn\_Click); } void removeHandler() { log("REMOVE HANDLER "+handlers); handlers--; try {btn.Click-=new EventHandler(btn\_Click); } catch { log("failed to remove handler"); } } void btn\_Click(object sender, EventArgs e) { clicks++; log(" got click "+clicks); }
and it resulted in the following log:
15.109 CLICK
15.125 ADD HANDLER 1
15.140 CLICK
15.156 got click 1
15.203 ADD HANDLER 2
15.218 CLICK
15.296 got click 2
15.312 got click 3
15.328 REMOVE HANDLER 2
15.343 CLICK
15.343 got click 4
15.437 REMOVE HANDLER 1
15.468 CLICK
15.484 REMOVE HANDLER 0
15.593 CLICKSo each -= effectively removes exactly one matching delegate if it finds one, and does not throw otherwise. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Luc Pattyn wrote:
The magic is you create a new object to remove an old (but very similar) object !
I'd always thought you needed to save the old one to unregister it. Learn something new everyday. :) How does this work if buggy code results in the same handler being registered twice?
-- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
Hi Dan, FYI: I just finised turning this subject into a short article. You may want to skim it. Regards
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }