For some reason, ThreadPool QueueWorkItem does not return an IAsyncResult or any other work item descriptor, which allows it to wait until completion. There are RegisterWait... methods, but you have to go through WaitHandle , and creating them is expensive (see IAsyncResult Documentation, which recommends deferring WaitHandle creation until requested). A parallel task library will eliminate this shortcoming, but there is a long wait for it. 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 question about the problems encountered when using async delegates instead of ThreadPool .
multithreading c # asynchronous concurrency threadpool
Alexey romanov
source share