c# - 您是否必须为异步方法调用调用 EndInvoke (或定义回调),即使它返回时您无事可做

标签 c# .net vb.net asynchronous callback

我在 CodeProject 上找到了以下关于异步调用方法的代码片段...... http://www.codeproject.com/Articles/14931/Asynchronous-Method-Invocation

private void CallFooWithOutAndRefParameters()
{
    // create the paramets to pass to the function
    string strParam1 = "Param1";
    int intValue = 100;
    ArrayList list = new ArrayList();
    list.Add("Item1");

    // create the delegate
    DelegateWithOutAndRefParameters delFoo =
      new DelegateWithOutAndRefParameters(FooWithOutAndRefParameters);

    // call the beginInvoke function!
    IAsyncResult tag =
        delFoo.BeginInvoke(strParam1,
            out intValue,
            ref list,
            null, null);

    // normally control is returned right away,
    // so you can do other work here...

    // calling end invoke notice that intValue and list are passed
    // as arguments because they might be updated within the function.
    string strResult =
        delFoo.EndInvoke(out intValue, ref list, tag);

    // write down the parameters:
    Trace.WriteLine("param1: " + strParam1);
    Trace.WriteLine("param2: " + intValue);
    Trace.WriteLine("ArrayList count: " + list.Count);
    Trace.WriteLine("return value: " + strResult);
}

这段代码有几处我不明白。

根据注释,控件在到达 BeginInvoke 行时会立即返回到调用代码。

这是否意味着后面的代码(EndInvoke 后跟一些跟踪日志记录)仅在 FooWithOutAndRefParameters 调用完成后...自动运行(即使该代码位于同一方法中)。我看起来有点困惑。 (我一直对这种事情使用回调。)

使用此方法我必须调用 EndInvoke。我可以只异步调用该方法而忘记它发生了吗?这有什么缺点吗?

如果我不调用 EndInvoke(如本方法所示),我是否应该始终有一个回调?即使回调什么也不做。

如果答案是您应该...那么您是调用 EndInvoke 还是定义回调? (定义回调的好处是您会收到结果通知)

顺便说一句,我知道我可以在 EndInvoke 或回调中检查错误或记录结果(事实上我可能会这样做)。我想知道的是,不调用 EndInvoke 或定义回调(例如内存泄漏)是否存在风险?最佳做法是什么。

赛斯

最佳答案

是的,您必须调用 EndInvoke()。不这样做会导致相当严重的资源泄漏,持续 10 分钟。底层管道是 .NET Remoting,10 分钟是“默认生命周期租用时间”。经常这样做,你的程序就会崩溃。让代表目标花费超过 10 分钟也有有趣的结果。

但最重要的是,如果不是调用操作的结果,您需要查明调用的方法是否成功完成。如果它死于异常,那么在调用 EndInvoke() 之前您不会发现它。此时再次引发异常。实际上处理这个异常是相当棘手的,因为你真的不知道你的程序状态在它爆炸之前有多少被委托(delegate)目标改变了。如果你真的想捕获它,目标没有太多的副作用是非常重要的。或者相反,目标捕获并重新抛出异常,根据需要执行状态恢复。

当然,您使用的示例非常愚蠢,很难在 one 方法中的 BeginInvoke 和 EndInvoke 调用之间做任何有用的事情。您始终依赖于您可以注册的回调,即 BeginInvoke 调用中倒数第二个参数。或者换句话说,不要像示例代码那样传递 null。并喜欢像 BackgroundWorker 和 Task 这样的类,甚至 ThreadPool.QueueUserWorkItem() 来消除这段代码的刺痛。使用委托(delegate)的 BeginInvoke() 方法是非常低级的黑客攻击。

委托(delegate)是 .NET 中非常强大的抽象,但它们并没有很好地分配其功能。使用它们来实现事件是样板和无故障的。正确使用它的 BeginInvoke() 方法是一门黑带艺术。一个值得注意的细节是它不再适用于 .NETCore,对远程处理的支持已从 CoreCLR 中删除。

关于c# - 您是否必须为异步方法调用调用 EndInvoke (或定义回调),即使它返回时您无事可做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620310/

相关文章:

.net - 为什么 AutoMapper v3 不能工作,因为它正在寻找 v2.2.1.0?

c# - Visual Studio WLS2 调试和命令行参数

c# - 将对象列表转换为接口(interface)列表

.net - GDI+ 如何更改线条平滑模式?

vb.net - 从 VB.Net 中的 app.config 获取动态更新的连接字符串

c# - 在哈希表中按值获取键 C#

c# - 我必须停止 System.Timers.Timer 吗?

vb.net - 是否可以在 VB.NET WinForms 项目的子命名空间中使用表单?

c# - 查找c#中某个子字符串后面出现的第一个数字的索引

c# - 简单集合的有序交换