简体   繁体   中英

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.QueueUserWorkItem(
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM