For whatever reason, ThreadPool
's QueueWorkItem
doesn't return an IAsyncResult
or some other handle to the work item, which would allow to wait until it's completed. There are RegisterWait...
methods, but you have to pass a WaitHandle
and creating them is expensive (see IAsyncResult
documentation, which advises you to delay creating a WaitHandle
until requested). The Task Parallel Library will fix this lack, but there is a long wait before that's available. So, are there any problems with this design:
public class Concurrent<T> {
private ManualResetEvent _resetEvent;
private T _result;
public Concurrent(Func<T> f) {
ThreadPool.QueueUserWorkItem(_ => {
_result = f();
if (_resetEvent != null)
_resetEvent.Set();
});
}
public WaitHandle WaitHandle {
get {
if (_resetEvent == null)
_resetEvent = new ManualResetEvent(_result != null);
return _resetEvent;
}
...
EDIT: I asked a follow-up question about the concerns which arise when using async delegates instead of the ThreadPool.