I know that in C#, there are several built in events that pass a parameter ("Cancel") which if set to true will stop further execution in the object that raised the event.
How would you implement an event where the raising object was able to keep track of a property in the EventArgs?
Here is a WinForms example of what I am trying to do:
http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx
Thank you.
It's really easy.
private event _myEvent; // ... // Create the event args CancelEventArgs args = new CancelEventArgs(); // Fire the event _myEvent.DynamicInvoke(new object[] { this, args }); // Check the result when the event handler returns if (args.Cancel) { // ... }
Here is the way that I avoid calling further subscribers once one of them asserts cancel:
var tmp = AutoBalanceTriggered; if (tmp != null) { var args = new CancelEventArgs(); foreach (EventHandler<CancelEventArgs> t in tmp.GetInvocationList()) { t(this, args); if (args.Cancel) // a client cancelled the operation { break; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With