Await on event handler

前端 未结 1 985
旧时难觅i
旧时难觅i 2021-02-14 17:47

So here\'s deleagte and event

public delegate Task SomeEventHandler(SomeEventArgs e);

...


public event SomeEventHandler OnSomething;

Subscri

1条回答
  •  悲&欢浪女
    2021-02-14 18:39

    The problem here is that multiple instances of SomeEventHandler are running hence there are multiple Task values being created. The await call is only running on one of them hence it's somewhat up to chance as to whether or not it's theDoSomething method that ends up being awaited.

    To fix this you will need to await on every Task value that is created

    if (this.OnSomething != null) {
      foreach (var d in this.OnSomething.GetInvocationList().Cast()) {
        await d(args);
      }
    ]
    

    0 讨论(0)
提交回复
热议问题