正确的方法来发射后不管的异步委托 [英] The correct way to Fire-and-Forget an asynchronous delegate

查看:121
本文介绍了正确的方法来发射后不管的异步委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我生锈的异步代表的主题。

Consider me rusty on the subject of asynchronous delegates.

如果我要异步调用的方法,在发射后不管的风格,这是一个适当的方式做到这一点?

If I want to call a method asynchronously, in a fire-and-forget style, is this an appropriate way to do it?

Action action = DoSomething;
action.BeginInvoke(action.EndInvoke, null);

DoSomething的()方法捕获所有异常,并处理他们内部。

The DoSomething() method catches all exceptions and deals with them internally.

是调用 EndInvoke会合适?需要?

有一个更清晰的方式来实现相同的行为?

Is there a clearer way to achieve the same behaviour?

推荐答案

在.NET 3.5的老派的方式是使用线程池

The "old-school" way in .NET 3.5 is to use the ThreadPool:

ThreadPool.QueueUserWorkItem(s => DoSomething());

如果您preFER使用异步委托,那么你应该知道,调用 EndInvoke会 必要的,即使你不'T有任何额外的code您希望在回调执行。

If you prefer to use asynchronous delegates, then you should know that the call to EndInvoke is necessary, even if you don't have any additional code you wish to execute on callback.

这篇关于正确的方法来发射后不管的异步委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆