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. CancelEventHandler?

CancelEventHandler?

Scheduled Pinned Locked Moved C#
questionlounge
7 Posts 3 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.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hello! How do CancelEventHandlers work? I think the delegates get called in a random order. What if the first delegate in the chain sets the Cancel property to true, but the second one resets it to false? Will the caller (i.e. the one who sent the event) get true or false in this case? Best regards Dominik


    _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

    L 1 Reply Last reply
    0
    • D Dominik Reichl

      Hello! How do CancelEventHandlers work? I think the delegates get called in a random order. What if the first delegate in the chain sets the Cancel property to true, but the second one resets it to false? Will the caller (i.e. the one who sent the event) get true or false in this case? Best regards Dominik


      _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Dominik Reichl wrote:

      How do CancelEventHandlers work? I think the delegates get called in a random order. What if the first delegate in the chain sets the Cancel property to true, but the second one resets it to false? Will the caller (i.e. the one who sent the event) get true or false in this case?

      You are correct :) Each 'handler' would have to check if the value is already set or not, decide on some policy how you will handling that. Obviously, if someone else attaches to it too, you will have no control. A better way would be to make your own 'CancelEventArgs', eg:

      class CancelEventArgs
      {
      bool cancel = false;

      public bool Cancel
      {
      get {return cancel;}
      set {if (!cancel) cancel = value;}
      }
      }

      This way you can gaurentee, once cancelled, it cant be uncancelled.

      **

      xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

      **

      D V 2 Replies Last reply
      0
      • L leppie

        Dominik Reichl wrote:

        How do CancelEventHandlers work? I think the delegates get called in a random order. What if the first delegate in the chain sets the Cancel property to true, but the second one resets it to false? Will the caller (i.e. the one who sent the event) get true or false in this case?

        You are correct :) Each 'handler' would have to check if the value is already set or not, decide on some policy how you will handling that. Obviously, if someone else attaches to it too, you will have no control. A better way would be to make your own 'CancelEventArgs', eg:

        class CancelEventArgs
        {
        bool cancel = false;

        public bool Cancel
        {
        get {return cancel;}
        set {if (!cancel) cancel = value;}
        }
        }

        This way you can gaurentee, once cancelled, it cant be uncancelled.

        **

        xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

        **

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #3

        I'll do it like that, thanks!


        _outp(0x64, 0xAD); and __asm mov al, 0xAD __asm out 0x64, al do the same... but what do they do?? ;) (doesn't work on NT)

        1 Reply Last reply
        0
        • L leppie

          Dominik Reichl wrote:

          How do CancelEventHandlers work? I think the delegates get called in a random order. What if the first delegate in the chain sets the Cancel property to true, but the second one resets it to false? Will the caller (i.e. the one who sent the event) get true or false in this case?

          You are correct :) Each 'handler' would have to check if the value is already set or not, decide on some policy how you will handling that. Obviously, if someone else attaches to it too, you will have no control. A better way would be to make your own 'CancelEventArgs', eg:

          class CancelEventArgs
          {
          bool cancel = false;

          public bool Cancel
          {
          get {return cancel;}
          set {if (!cancel) cancel = value;}
          }
          }

          This way you can gaurentee, once cancelled, it cant be uncancelled.

          **

          xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

          **

          V Offline
          V Offline
          Vitaliy Tsvayer
          wrote on last edited by
          #4

          You could also do something like this:

             public class TestCancellableEvents
             {
                 public static void Main()
                 {
                      TestClass test = new TestClass();
                      test.MyEvent += new MyDelegate(test_MyEvent);
                      test.MyEvent += new MyDelegate(test_MyEvent2);
          
                      test.doit();
          	}
          
                  void test_MyEvent2(object sender, CancelEventArgs args)
                  {
                     //do nothing this won't be called
                  }
          
                  void test_MyEvent(object sender, CancelEventArgs args)
                  {
                      //Cancel event, so the following handlers in the chain won't be called
                      args.Cancel = true;
                  }
          
              public class CancelEventArgs
              {
                  bool cancel = false;
          
                  public bool Cancel
                  {
                      get { return cancel; }
                      set { cancel = value; }
                  }
              }
          
              public delegate void MyDelegate(object sender, CancelEventArgs args);
              public class TestClass
              {
                  public event MyDelegate MyEvent;
          
                  public void doit()
                  {
                      Delegate[] list = MyEvent.GetInvocationList();
                      CancelEventArgs args = new CancelEventArgs();
                      foreach (MyDelegate handler in list)
                      {
                          handler(this, args);
                          if (args.Cancel)
                          {
                              break;
                          }
                      }
                  }
              }
          

          Vitaliy Tsvayer Tikle

          L 1 Reply Last reply
          0
          • V Vitaliy Tsvayer

            You could also do something like this:

               public class TestCancellableEvents
               {
                   public static void Main()
                   {
                        TestClass test = new TestClass();
                        test.MyEvent += new MyDelegate(test_MyEvent);
                        test.MyEvent += new MyDelegate(test_MyEvent2);
            
                        test.doit();
            	}
            
                    void test_MyEvent2(object sender, CancelEventArgs args)
                    {
                       //do nothing this won't be called
                    }
            
                    void test_MyEvent(object sender, CancelEventArgs args)
                    {
                        //Cancel event, so the following handlers in the chain won't be called
                        args.Cancel = true;
                    }
            
                public class CancelEventArgs
                {
                    bool cancel = false;
            
                    public bool Cancel
                    {
                        get { return cancel; }
                        set { cancel = value; }
                    }
                }
            
                public delegate void MyDelegate(object sender, CancelEventArgs args);
                public class TestClass
                {
                    public event MyDelegate MyEvent;
            
                    public void doit()
                    {
                        Delegate[] list = MyEvent.GetInvocationList();
                        CancelEventArgs args = new CancelEventArgs();
                        foreach (MyDelegate handler in list)
                        {
                            handler(this, args);
                            if (args.Cancel)
                            {
                                break;
                            }
                        }
                    }
                }
            

            Vitaliy Tsvayer Tikle

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Vitaliy Tsvayer wrote:

            Delegate[] list = MyEvent.GetInvocationList();

            You sure that doesnt return multicast delegates? Good approach :)

            **

            xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

            **

            V 1 Reply Last reply
            0
            • L leppie

              Vitaliy Tsvayer wrote:

              Delegate[] list = MyEvent.GetInvocationList();

              You sure that doesnt return multicast delegates? Good approach :)

              **

              xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

              **

              V Offline
              V Offline
              Vitaliy Tsvayer
              wrote on last edited by
              #6

              Well,public MyDelegate MyEvent;
              is actually compiled as following:private MyDelegate MyEvent; [MethodImpl(MethodImplOptions.Synchronized)] public void add_MyEvent(MyDelegate value) { this.MyEvent = (MyDelegate) Delegate.Combine(this.MyEvent, value); } [MethodImpl(MethodImplOptions.Synchronized)] public void remove_MyEvent(MyDelegate value) { this.MyEvent = (MyDelegate) Delegate.Remove(this.MyEvent, value); }
              that is evet keyoword in c# creates methods to manage multicast delegate. And the following:class1.MyEvent += new MyDelegate(this.test_MyEvent);
              is simply a call to the add_MyEvent() method. And finally GetInvocationList() method is defined in MSDN as below: "Returns an array of delegates representing the invocation list of the current delegate. Each delegate in the array represents exactly one method. The order of the delegates in the array is the same order in which the current delegate invokes the methods that those delegates represent."

              Vitaliy Tsvayer Tikle

              L 1 Reply Last reply
              0
              • V Vitaliy Tsvayer

                Well,public MyDelegate MyEvent;
                is actually compiled as following:private MyDelegate MyEvent; [MethodImpl(MethodImplOptions.Synchronized)] public void add_MyEvent(MyDelegate value) { this.MyEvent = (MyDelegate) Delegate.Combine(this.MyEvent, value); } [MethodImpl(MethodImplOptions.Synchronized)] public void remove_MyEvent(MyDelegate value) { this.MyEvent = (MyDelegate) Delegate.Remove(this.MyEvent, value); }
                that is evet keyoword in c# creates methods to manage multicast delegate. And the following:class1.MyEvent += new MyDelegate(this.test_MyEvent);
                is simply a call to the add_MyEvent() method. And finally GetInvocationList() method is defined in MSDN as below: "Returns an array of delegates representing the invocation list of the current delegate. Each delegate in the array represents exactly one method. The order of the delegates in the array is the same order in which the current delegate invokes the methods that those delegates represent."

                Vitaliy Tsvayer Tikle

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Thanks for the MSDN snippet. Never seen or noticed that, or maybe they added it later. It's good info to know. :)

                **

                xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!

                **

                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