如何等待C#中的事件? [英] How do I await events in C#?

查看:213
本文介绍了如何等待C#中的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个具有一系列事件的类,其中一个事件是GameShuttingDown.当触发此事件时,我需要调用事件处理程序.该事件的目的是通知用户游戏正在关闭,并且他们需要保存其数据.可以等待保存,而不能等待事件.因此,当处理程序被调用时,游戏将在等待的处理程序完成之前关闭.

I am creating a class that has a series of events, one of them being GameShuttingDown. When this event is fired, I need to invoke the event handler. The point of this event is to notify users the game is shutting down and they need to save their data. The saves are awaitable, and events are not. So when the handler gets called, the game shuts down before the awaited handlers can complete.

public event EventHandler<EventArgs> GameShuttingDown;

public virtual async Task ShutdownGame()
{
    await this.NotifyGameShuttingDown();

    await this.SaveWorlds();

    this.NotifyGameShutDown();
}

private async Task SaveWorlds()
{
    foreach (DefaultWorld world in this.Worlds)
    {
        await this.worldService.SaveWorld(world);
    }
}

protected virtual void NotifyGameShuttingDown()
{
    var handler = this.GameShuttingDown;
    if (handler == null)
    {
        return;
    }

    handler(this, new EventArgs());
}

事件注册

// The game gets shut down before this completes because of the nature of how events work
DefaultGame.GameShuttingDown += async (sender, args) => await this.repo.Save(blah);

我知道事件的签名是void EventName,因此使它异步基本上是生死攸关的.我的引擎大量使用事件通知第三者开发人员(和多个内部组件)事件正在引擎内发生,并让事件对事件做出反应.

I understand that the signature for events are void EventName and so making it async is basically fire and forget. My engine makes heavy use of eventing to notify 3rd party developers (and multiple internal components) that events are taking place within the engine and letting them react to them.

是否有一条很好的途径可以将事件替换为我可以使用的基于异步的东西?我不确定是否应该在回调中使用BeginShutdownGameEndShutdownGame,但这很痛苦,因为只有调用源才能传递回调,而没有任何插入到引擎中的第三方工具我从事件中得到什么.如果服务器调用game.ShutdownGame(),则引擎插件和引擎中的其他组件无法传递其回调,除非我采用某种注册方法,以保留回调的集合.

Is there a good route to go down to replace eventing with something asynchronous based that I can use? I'm not sure if I should be using BeginShutdownGame and EndShutdownGame with callbacks, but that's a pain because then only the calling source can pass a callback, and not any 3rd party stuff that plugs in to the engine, which is what I am getting with events. If the server calls game.ShutdownGame(), there's no way for engine plugins and or other components within the engine to pass along their callbacks, unless I wire up some kind of registration method, keeping a collection of callbacks.

任何有关采用哪种首选/推荐路线的建议,将不胜感激!我环顾四周,大部分情况下看到的是使用开始/结束方法,我认为这不会满足我想要做的事情.

Any advice on what the preferred/recommended route to go down with this would be greatly appreciated! I have looked around and for the most part what I've seen is using the Begin/End approach which I don't think will satisfy what I'm wanting to do.

修改

我正在考虑的另一个选项是使用注册方法,该方法需要等待回调.我遍历所有回调,获取它们的Task并等待WhenAll.

Another option I'm considering is using a registration method, that takes an awaitable callback. I iterate over all of the callbacks, grab their Task and await with a WhenAll.

private List<Func<Task>> ShutdownCallbacks = new List<Func<Task>>();

public void RegisterShutdownCallback(Func<Task> callback)
{
    this.ShutdownCallbacks.Add(callback);
}

public async Task Shutdown()
{
    var callbackTasks = new List<Task>();
    foreach(var callback in this.ShutdownCallbacks)
    {
        callbackTasks.Add(callback());
    }

    await Task.WhenAll(callbackTasks);
}

推荐答案

就我个人而言,我认为拥有async事件处理程序可能不是最佳的设计选择,其中至少一个原因是您所遇到的问题.使用同步处理程序,知道它们何时完成很简单.

Personally, I think that having async event handlers may not be the best design choice, not the least of which reason being the very problem you're having. With synchronous handlers, it's trivial to know when they complete.

也就是说,如果由于某种原因您必须或者至少强烈被迫坚持这种设计,则可以以await友好的方式来实现.

That said, if for some reason you must or at least are strongly compelled to stick with this design, you can do it in an await-friendly way.

您的想法来注册处理程序,并await他们是一个好主意.但是,我建议您坚持使用现有的事件范例,因为这将使事件在代码中保持可表达性.最主要的是,您必须偏离基于标准EventHandler的委托类型,并使用返回Task的委托类型,以便可以await处理程序.

Your idea to register handlers and await them is a good one. However, I would suggest sticking with the existing event paradigm, as that will keep the expressiveness of events in your code. The main thing is that you have to deviate from the standard EventHandler-based delegate type, and use a delegate type that returns a Task so that you can await the handlers.

这是一个简单的例子,说明了我的意思:

Here's a simple example illustrating what I mean:

class A
{
    public event Func<object, EventArgs, Task> Shutdown;

    public async Task OnShutdown()
    {
        Func<object, EventArgs, Task> handler = Shutdown;

        if (handler == null)
        {
            return;
        }

        Delegate[] invocationList = handler.GetInvocationList();
        Task[] handlerTasks = new Task[invocationList.Length];

        for (int i = 0; i < invocationList.Length; i++)
        {
            handlerTasks[i] = ((Func<object, EventArgs, Task>)invocationList[i])(this, EventArgs.Empty);
        }

        await Task.WhenAll(handlerTasks);
    }
}

OnShutdown()方法在执行标准的获取事件委托实例的本地副本"之后,首先调用所有处理程序,然后等待所有返回的Tasks(已将它们保存为本地数组处理程序被调用).

The OnShutdown() method, after doing the standard "get local copy of the event delegate instance", first invokes all of the handlers, and then awaits all of the returned Tasks (having saved them to a local array as the handlers are invoked).

这是一个简短的控制台程序,说明了用法:

Here's a short console program illustrating the use:

class Program
{
    static void Main(string[] args)
    {
        A a = new A();

        a.Shutdown += Handler1;
        a.Shutdown += Handler2;
        a.Shutdown += Handler3;

        a.OnShutdown().Wait();
    }

    static async Task Handler1(object sender, EventArgs e)
    {
        Console.WriteLine("Starting shutdown handler #1");
        await Task.Delay(1000);
        Console.WriteLine("Done with shutdown handler #1");
    }

    static async Task Handler2(object sender, EventArgs e)
    {
        Console.WriteLine("Starting shutdown handler #2");
        await Task.Delay(5000);
        Console.WriteLine("Done with shutdown handler #2");
    }

    static async Task Handler3(object sender, EventArgs e)
    {
        Console.WriteLine("Starting shutdown handler #3");
        await Task.Delay(2000);
        Console.WriteLine("Done with shutdown handler #3");
    }
}

经历了这个示例之后,我现在发现自己想知道C#是否没有办法将其抽象一些.更改可能太复杂了,但是旧式void返回事件处理程序和新的async/await功能的当前组合确实有些尴尬.上面的方法可以正常工作(恕我直言,效果很好),但是对场景有更好的CLR和/或语言支持(即能够等待多播委托并使C#编译器将其转换为对WhenAll()).

Having gone through this example, I now find myself wondering if there couldn't have been a way for C# to abstract this a bit. Maybe it would have been too complicated a change, but the current mix of the old-style void-returning event handlers and the new async/await feature does seem a bit awkward. The above works (and works well, IMHO), but it would have been nice to have better CLR and/or language support for the scenario (i.e. be able to await a multicast delegate and have the C# compiler turn that into a call to WhenAll()).

这篇关于如何等待C#中的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆