In .net 3.5
trying to ThreadPool.QueueUserWorkItem(a=> {Work()});
when the ThreadPool has no available threads caused BeginInvoke lock up.
void Work()
{
Action executor = () = { DoSomething(); };
IAsyncResult result = executor.BeginInvoke(null, null);
using (WaitHandle hWait = result.AsyncWaitHandle)
{
if (hWait.WaitOne(timeoutMilliseconds))
{
executor.EndInvoke(result);
}
else
{ throw new ImDyingException(); }
}
}
How can I make the BeginInvoke use a non-pooled thread?