是否有可能组/使用WaitHandle.WaitAll的分离时,在线程池的任务?有可能、线程、任务、WaitAll

2023-09-06 08:56:06 作者:望江中两畔,渔火明照,照亮一抹身影。

我现在面临的情况是如下。因为线程池每个进程1个实例,所以我的问题是,将方法1取消在3秒后排队方法2任务

  

http请求进来

  *方法1最先被执行*:

  ThreadPool.QueueUserWorkItem×3
  WaitHandle.WaitAll的3秒

*方法2法1 *之后执行的:

  ThreadPool.QueueUserWorkItem×10
  WaitHandle.WaitAll的10秒
 

对不起,我觉得我完全误解了使用的WaitHandle的。看来,如果我做下的所有内容将正常工作所需。很抱歉的混乱。

  VAR通话=新的ManualResetEvent [5];
//ThreadPool.QueueUserWorkItem等等...
WaitHandle.WaitAll的(来电,超时);
 

但我还在想会发生什么事时,方法1充斥着长时间运行的任务和方法2只等待1秒的线程池。威尔法2曾经得到的结果返回,因为它没有等待足够长的时间。的

感谢。

解决方案 更新香奈儿2018新春彩妆啦 快来买买买 及大量赠品

没有,也不会取消任务。只要你宁愿停止等待。顺便说一句,不而是一个超时异常时,可以为WaitAll超过超时抛出?

The scenario I am facing is as below. Because ThreadPool is 1 instance per process so my question is that would method 1 cancel tasks queued by method 2 after 3 seconds?

http request comes in

*method 1 gets executed first*:

  ThreadPool.QueueUserWorkItem x 3
  WaitHandle.WaitAll for 3 seconds

*method 2 gets executed after method 1*:

  ThreadPool.QueueUserWorkItem x 10
  WaitHandle.WaitAll for 10 seconds

Sorry I think I totally misunderstood the use of WaitHandle. It seems that if I do below everything will work as desired. So sorry for the confusion.

var calls = new ManualResetEvent[5];
//ThreadPool.QueueUserWorkItem blah...
WaitHandle.WaitAll(calls, timeOut);

But I am still thinking what will happen when method 1 flooded thread pool with long running tasks and method 2 only waits for 1 second. Will method 2 ever get its results back because it's not waiting long enough.

Thanks.

解决方案

No, it wouldn't cancel the tasks. Simply you'd rather stop waiting. BTW, wouldn't rather an timeout exception be thrown when WaitAll exceeds timeout?