如何在C#中实现取消事件

23
5个回答

21

非常简单。

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)
{
    // ...
}

2
非常明显。今天就是那种日子。非常感谢你。 - Sako73
@JonSeigel,我理解正确吗?如果有两个处理程序被注册,第一个将 args.Cancel 设为 true,第二个将 args.Cancel 设为 false,那么结果是 args.Cancel==false 吗? - thersch
@thersch:我认为是这样的,因为两个处理程序都会在同一个对象上设置值。不过你最好测试一下以确保。 - Jon Seigel

15

以下是我避免在一个订阅者取消后继续调用其他订阅者的方法:

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;
        }
    }
}

SO投票逻辑的又一次演示。 - Behrooz

4
我需要的是一种方法,使订阅者在取消事件后不再接收到它。在我的情况下,我不希望某个订阅者取消事件后,事件继续传播给其他订阅者。 我使用自定义事件处理来实现这一点:
public class Program
{
    private static List<EventHandler<CancelEventArgs>> SubscribersList = new List<EventHandler<CancelEventArgs>>();

    public static event EventHandler<CancelEventArgs> TheEvent
    {
        add {
            if (!SubscribersList.Contains(value))
            {
                SubscribersList.Add(value);
            }
        }
        remove
        {
            if (SubscribersList.Contains(value))
            {
                SubscribersList.Remove(value);
            }
        }
    }

    public static void RaiseTheEvent(object sender, CancelEventArgs cancelArgs)
    {
        foreach (EventHandler<CancelEventArgs> sub in SubscribersList)
        {
            sub(sender, cancelArgs);

            // Stop the Execution after a subscriber cancels the event
            if (cancelArgs.Cancel)
            {
                break;
            }
        }
    }


    static void Main(string[] args)
    {
        new Subscriber1();
        new Subscriber2();

        Console.WriteLine("Program: Raising the event");

        CancelEventArgs cancelArgs = new CancelEventArgs();
        RaiseTheEvent(null, cancelArgs);
        if (cancelArgs.Cancel)
        {
            Console.WriteLine("Program: The Event was Canceled");
        }
        else
        {
            Console.WriteLine("Program: The Event was NOT Canceled");
        }
        Console.ReadLine();
    }
}

public class Subscriber1
{
    public Subscriber1()
    {
        Program.TheEvent += new EventHandler<CancelEventArgs>(program_TheEvent);
    }

    void program_TheEvent(object sender, CancelEventArgs e)
    {
        Console.WriteLine("Subscriber1: in program_TheEvent");
        Console.WriteLine("Subscriber1: Canceling the event");
        e.Cancel = true;
    }
}

public class Subscriber2
{
    public Subscriber2()
    {
        Program.TheEvent += new EventHandler<CancelEventArgs>(program_TheEvent);
    }

    void program_TheEvent(object sender, CancelEventArgs e)
    {
        Console.WriteLine("Subscriber2: in program_TheEvent");

    }
}

2

简单易懂:

  1. 创建一个CancelEventArgs(或自定义类型)的实例。
  2. 引发事件并传递该实例。
  3. 检查[1]上的Canceld属性。

您需要代码示例吗?


0

你需要等待调用引发事件并在你的EventArgs(特别是CancelEventArgs)中检查标志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接