繁体   English   中英

如果ThreadPool已满,如何使Action.BeginInvoke生成非线程池线程

[英]If ThreadPool is full, how to make Action.BeginInvoke spawn a non threadpool thread

在.net 3.5中

尝试ThreadPool.QueueUserWorkItem(a=> {Work()}); 当ThreadPool没有可用线程导致BeginInvoke锁定时。

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(); }
   }
}

如何使BeginInvoke使用非池化线程?

你不能 听起来您好像在过度使用游泳池。 也许考虑节制队列/自定义线程池(即,同步的生产者/消费者队列)?

(不要增加池的大小;这几乎总是错误的方法)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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