且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中的异步事件

更新时间:2023-09-13 17:38:46

我个人认为有异步事件处理程序可能不是***的设计选择,而不是其原因是您遇到的问题非常最少的。使用同步处理,是微不足道的知道他们什么时候完成。



这是说,如果由于某种原因必须或至少是强烈***坚持使用这种设计,可以这样做在等待 - 友好的方式。



您的想法注册处理程序和等待他们来说是一个很好的一个。不过,我建议用现有的事件范式坚持,因为这将让事件的表现在你的代码。最主要的是,你必须从标准事件处理程序基于委托类型偏离,并使用返回任务委托类型这样就可以了等待处理程序



下面是说明我的意思是一个简单的例子:

  A级
{
公共事件Func键<对象,EventArgs的,任务>关闭;

公共异步任务OnShutdown()
{
Func键<对象,EventArgs的,任务>处理器=关机;

如果(处理程序== NULL)
{
的回报;
}
$ B代表[] = invocationList handler.GetInvocationList()$ B;
任务[] = handlerTasks新任务[invocationList.Length]

的for(int i = 0; I< invocationList.Length;我++)
{
handlerTasks [I] =((Func键<对象,EventArgs的,任务>)invocationList [I])(这一点,EventArgs.Empty);
}

等待Task.WhenAll(handlerTasks);
}
}



OnShutdown()的方法,这样做的标准后,获得事件委托实例的本地副本,首先调用所有的处理程序,然后等待所有返回的任务(让他们保存的处理程序调用的本地阵列)



下面的说明使用短控制台程序:

 类节目
{
静态无效的主要(字串[] args)
{
A A =新的A() ;

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

a.OnShutdown()等待()。
}

静态异步任务Handler1(对象发件人,EventArgs五)
{
Console.WriteLine(启动停机处理程序#1);
等待Task.Delay(1000);
Console.WriteLine(与关机处理器#1完成);
}

静态异步任务Handler2(对象发件人,EventArgs五)
{
Console.WriteLine(启动停机处理程序#2);
等待Task.Delay(5000);
Console.WriteLine(与停机处理程序#2完成);
}

静态异步任务Handler3(对象发件人,EventArgs五)
{
Console.WriteLine(启动停机处理程序#3);
等待Task.Delay(2000);
Console.WriteLine(与停机处理程序#3完成);
}
}



通过这个例子已经走了,现在我发现自己想知道如果不能对C#是一个方法,这个抽象一点。也许它会被太复杂的变化,但目前旧式的搭配无效 -returning事件处理程序和新的异步 / 的await 功能似乎有点尴尬。以上作品(并且效果很好,恕我直言),但它本来不错的的情况下更好的CLR和/或语言支持(即能够等候一个多播委托,并有C#编译器把它转换成一个调用 WhenAll())。


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

Event registration

// 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);

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.

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.

Edit

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

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.

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.

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

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

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()).