CancelEventHandler?
-
Hello! How do
CancelEventHandler
s work? I think the delegates get called in a random order. What if the first delegate in the chain sets theCancel
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) -
Hello! How do
CancelEventHandler
s work? I think the delegates get called in a random order. What if the first delegate in the chain sets theCancel
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)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!
**
-
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!
**
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) -
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!
**
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
-
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
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!
**
-
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!
**
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
-
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
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!
**