开发者

ThreadPool.QueueUserWorkItem and EndInvoke

Do I need to call the EndInvoke on a delegate passed to ThreadPool.QueueUserWorkItem?

I am using this suggestion to call the ThreadPoolQueueWorkItem.

ThreadPool.QueueUserWo开发者_C百科rkItem(
    delegate { someDelegate(arg1, arg2); }
);

If i need to call endinvoke, how do I call it? Thanks.


No, you don't need to call it. In fact, you shouldn't :-)

You should only call EndInvoke to end an asynchronous invocation previously started by calling BeginInvoke.

In the code you have provided you are running the delegate synchronously from a ThreadPool thread.

This approach is just fine when you have a "fire and forget" requirement. However, if you need to retrieve output from the invocation it would be better to use BeginInvoke followed by EndInvoke.


You don't need to, no, but according to MSDN you should to avoid potential memory leaks. Here's a great blog post by Phil Haack discussing the issue, as well as his helper class for fire and forget use cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜