How to implement a cancel event in C#

后端 未结 5 1477
星月不相逢
星月不相逢 2021-02-12 12:28

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 eve

5条回答
  •  北恋
    北恋 (楼主)
    2021-02-12 12:57

    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 t in tmp.GetInvocationList())
        {
            t(this, args);
            if (args.Cancel)  // a client cancelled the operation
            {
                break;
            }
        }
    }
    

提交回复
热议问题