没有 EndInvoke 的 C# 异步调用?

C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
本文介绍了没有 EndInvoke 的 C# 异步调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以以下类为例.

public class A
{
   // ...
   void Foo(S myStruct){...}
}

public class B
{
   public A test;
   // ...
   void Bar()
   {
      S myStruct = new S();
      test.Foo(myStruct);
   }
}

现在,我希望方法调用 test.Foo(myStruct) 是一个异步调用(即发即弃").bar-method 需要尽快返回.关于委托、BeginInvoke、EndInvoke、ThreadPool 等的文档并没有帮助我找到解决方案.

Now, I want the method-call test.Foo(myStruct) to be an asynchronous call ('fire-and-forget'). The bar-method needs to return as soon as possible. Documentation around delegates, BeginInvoke, EndInvoke, the ThreadPool etc. isn't helping me find a solution.

这是一个有效的解决方案吗?

Is this a valid solution?

     // Is using the `EndInvoke` method as the callback delegate valid?
     foo.BeginInvoke(myStruct, foo.EndInvoke, null);

推荐答案

你不需要调用 EndInvoke;不叫它仅仅意味着:

You are not required to call EndInvoke; not calling it merely means:

  • 您没有从方法中获得返回值.
  • 在方法执行过程中抛出的任何异常都会消失.

听起来您想一劳永逸",所以最简单的方法是使用匿名委托,例如:

It sounds like you want to 'fire-and-forget', so the easiest way to do this is to use an anonymous delegate, for example:

var del = new Action(foo.Bar);
del.BeginInvoke(iar =>
{
   try
   {
      del.EndInvoke(iar);
   }
   catch (Exception ex)
   {
      // Log the message?
   }
}, null);

当你执行这段代码时会发生这种情况:

This is what happens when you execute this code:

  1. 为委托分配(简单地说)一个新线程.
  2. 线程被赋予了委托 del 和匿名委托 (iar => ...).
  3. 线程执行del.
  4. 当它完成执行(或发生异常)时,将存储结果或异常并执行匿名委托.
  5. 在匿名委托中,当调用 EndInvoke 时,要么返回方法的结果,要么抛出异常(如果发生异常).
  1. A new thread is allocated (put simply) for the delegate.
  2. The thread is given the delegate del and the anonymous delegate (iar => ...).
  3. The thread executes del.
  4. When it is finished executing (or an exception occurs) the result or exception is stored and the anonymous delegate is executed.
  5. Inside the anonymous delegate, when EndInvoke is called the result from the method is either returned, or the exception is thrown (if one occurred).

请注意,上面的示例与:

Note that the above example is very different from:

// This is pointless and is still, essentially, synchronous.
del.EndInvoke(del.BeginInvoke(null, null));

您应该始终调用End*.我从来没有发现不调用它会出现问题的场景,但这是一个实现细节,并且是 依赖于未记录的行为.

You should always call End*. I've never found a scenario where not calling it presents a problem, however that is an implementation detail and is relying on undocumented behavior.

最后,如果抛出异常,您的解决方案将使进程崩溃,如果您不关心异常,您可以简单地将 null 作为委托传递 (del.BeginInvoke(myStruct, null, null);). 作为最后一个示例,您正在寻找的可能是:

Finally your solution would crash the process if an exception is thrown, you can simply pass null as the delegate if you don't care about the exception (del.BeginInvoke(myStruct, null, null);). So as a final example what you are looking for is probably:

public class A
{
    // ...
    void Foo(S myStruct){...}
    void FooAsync(S myStruct)
    {
        var del = new Action<S>(Foo);
        del.BeginInvoke(myStruct, SuppressException, del);
    }

    static void SuppressException(IAsyncResult ar)
    {
        try
        {
            ((Action<S>)ar.AsyncState).EndInvoke(ar);
        }
        catch
        {
            // TODO: Log
        }
    }
}

这篇关于没有 EndInvoke 的 C# 异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)