Cancel handlers. Cancel a property on CancelEventArgs

CancelEventArgs provides a Cancel property, which can be handled by different event handlers to tell the object that raised the event whether to continue execution cancel the action.

It appears that since events are multicast delegates, a simple raise of an event can trigger two delegates. The first sets the Cancel property to true, and the second sets the Cancel property to false. Do these events support component cancellation / cancellation scenarios, and do each delegate invoke one by one, checking the cancel flag at each step? What is the best practice for enhancing these types of events? Only one instance of CancelEventArgs passed to each delegate? Are individual instances used?

+3
source share
2 answers

A small experiment quickly shows that they use a single instance of Cancel (possibly an EventArgs object).

, , Eventhandlers.

CancelEventArgs , , .

+2

:

public static void Main() {
    Event += (sender, e) => e.Cancel = true;
    Event += (sender, e) => e.Cancel = false;
    Event += (sender, e) => e.Cancel = true;

    var args = new CancelEventArgs();
    Event(null, args);

    WL(args.Cancel);
}

static event EventHandler<CancelEventArgs> Event;

.

, args multicast , .NET .

+1

Source: https://habr.com/ru/post/1704091/


All Articles