<legend id='B8Wok'><style id='B8Wok'><dir id='B8Wok'><q id='B8Wok'></q></dir></style></legend>

    1. <small id='B8Wok'></small><noframes id='B8Wok'>

      <tfoot id='B8Wok'></tfoot>

        <bdo id='B8Wok'></bdo><ul id='B8Wok'></ul>
      <i id='B8Wok'><tr id='B8Wok'><dt id='B8Wok'><q id='B8Wok'><span id='B8Wok'><b id='B8Wok'><form id='B8Wok'><ins id='B8Wok'></ins><ul id='B8Wok'></ul><sub id='B8Wok'></sub></form><legend id='B8Wok'></legend><bdo id='B8Wok'><pre id='B8Wok'><center id='B8Wok'></center></pre></bdo></b><th id='B8Wok'></th></span></q></dt></tr></i><div id='B8Wok'><tfoot id='B8Wok'></tfoot><dl id='B8Wok'><fieldset id='B8Wok'></fieldset></dl></div>
    2. 没有 EndInvoke 的 C# 异步调用?

      C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)

      <i id='Xu8wx'><tr id='Xu8wx'><dt id='Xu8wx'><q id='Xu8wx'><span id='Xu8wx'><b id='Xu8wx'><form id='Xu8wx'><ins id='Xu8wx'></ins><ul id='Xu8wx'></ul><sub id='Xu8wx'></sub></form><legend id='Xu8wx'></legend><bdo id='Xu8wx'><pre id='Xu8wx'><center id='Xu8wx'></center></pre></bdo></b><th id='Xu8wx'></th></span></q></dt></tr></i><div id='Xu8wx'><tfoot id='Xu8wx'></tfoot><dl id='Xu8wx'><fieldset id='Xu8wx'></fieldset></dl></div>
      <tfoot id='Xu8wx'></tfoot>
    3. <small id='Xu8wx'></small><noframes id='Xu8wx'>

          <tbody id='Xu8wx'></tbody>

          <bdo id='Xu8wx'></bdo><ul id='Xu8wx'></ul>
          <legend id='Xu8wx'><style id='Xu8wx'><dir id='Xu8wx'><q id='Xu8wx'></q></dir></style></legend>

              1. 本文介绍了没有 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# 异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                Func Delegate vs Function(函数委托与函数)
              2. <small id='NnBvX'></small><noframes id='NnBvX'>

                  • <bdo id='NnBvX'></bdo><ul id='NnBvX'></ul>
                      <tfoot id='NnBvX'></tfoot>

                          <tbody id='NnBvX'></tbody>
                        <legend id='NnBvX'><style id='NnBvX'><dir id='NnBvX'><q id='NnBvX'></q></dir></style></legend>

                        <i id='NnBvX'><tr id='NnBvX'><dt id='NnBvX'><q id='NnBvX'><span id='NnBvX'><b id='NnBvX'><form id='NnBvX'><ins id='NnBvX'></ins><ul id='NnBvX'></ul><sub id='NnBvX'></sub></form><legend id='NnBvX'></legend><bdo id='NnBvX'><pre id='NnBvX'><center id='NnBvX'></center></pre></bdo></b><th id='NnBvX'></th></span></q></dt></tr></i><div id='NnBvX'><tfoot id='NnBvX'></tfoot><dl id='NnBvX'><fieldset id='NnBvX'></fieldset></dl></div>