Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to unregister an event

How to unregister an event

Scheduled Pinned Locked Moved C#
helptutorial
12 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    t4ure4n
    wrote on last edited by
    #1

    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 event

    o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

    K L B 3 Replies Last reply
    0
    • T t4ure4n

      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 event

      o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      When you add an event handler you use += to remove one you use -= _WebClient.DocumentComplete -= new EventHandler(yourEventHandlerNameHere); Hope that helps. Ben

      1 Reply Last reply
      0
      • T t4ure4n

        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 event

        o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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] }


        D M 2 Replies Last reply
        0
        • T t4ure4n

          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 event

          o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

          B Offline
          B Offline
          b_umika
          wrote on last edited by
          #4

          Have you tried using _WebClient.DocumentComplete -= ; Is that what you are talking about?

          1 Reply Last reply
          0
          • L Luc Pattyn

            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] }


            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            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

            L B 4 Replies Last reply
            0
            • L Luc Pattyn

              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] }


              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • D Dan Neely

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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] }


                1 Reply Last reply
                0
                • M Martin 0

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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] }


                  M 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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] }


                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    Aha, that explains a few things. Now take that holiday !

                    :) :rose:

                    All the best, Martin

                    1 Reply Last reply
                    0
                    • D Dan Neely

                      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

                      B Offline
                      B Offline
                      Bijesh
                      wrote on last edited by
                      #10

                      dan neely wrote:

                      How does this work if buggy code results in the same handler being registered twice?

                      Its ugly. You basically get the same handler invoked twice on that event.. I've had to track down so many of these 'event leaks'.. :sigh:

                      1 Reply Last reply
                      0
                      • D Dan Neely

                        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

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        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 CLICK

                        So 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] }


                        1 Reply Last reply
                        0
                        • D Dan Neely

                          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

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          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] }


                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups