是否有可能组/使用WaitHandle.WaitAll的分离时,在线程池的任务? [英] Is it possible to group/isolate tasks in ThreadPool when using WaitHandle.WaitAll?

查看:190
本文介绍了是否有可能组/使用WaitHandle.WaitAll的分离时,在线程池的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临的情况是如下。因为线程池每个进程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曾经得到的结果返回,因为它没有等待足够长的时间。

感谢。

解决方案

没有,也不会取消任务。只要你宁愿停止等待。顺便说一句,不而是一个超时异常时,可以为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?

这篇关于是否有可能组/使用WaitHandle.WaitAll的分离时,在线程池的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆