views:

138

answers:

4

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.

+1  A: 

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?

I'm a bit confused So you want to run Method1 parallelly 3 times, and then Method2 parallelly 10 times? Or is the Method1 that launches 3 tasks and and then Method2 launches its 10 tasks? If so the code look like: { Method1(); WaitAll(); Method2();} and yes, Method2 waits for Method1.
+1  A: 

Hello,

I think, you should create your own queue+dispatcher to process a group your actions or tasks. The Active Object pattern is a good choose. You can control priority of execution of actions, can write the rule to wait some actions in group (by using Guard method), wait for "any" or "all" results. You can read this article, it has code to try this pattern in action.

Luck!

igor
Seems that it might take me some time to understand it.
Jeffrey C
+1  A: 

As other people have pointed out, when the wait is complete method 1 will not cancel the threads queued by method 2 unless you have some explicit code in method 1 that specifically cause method 2 to cancel.

You mentioned that you might have a race condition, but unless method 2 is relying on method 1 to complete in order to do some computation with the results in method 1, then there is no apparent race condition in your example.

Please clarify where you think you're seeing a race condition and why, from your current example it doesn't seem like you have a race condition.

Lirik
+1  A: 

In reference to your update:

Be aware if you're using WaitAll with a windows forms application that it will not work without setting your entry point to [MTAThread] (which the compiler fails on anyway).

This enhanced ThreadPool supports grouping of work items (and cancelling them) which may help if you're still looking for a solution.

Chris S
@Chris, thanks and I will have a look. But I thought according to http://msdn.microsoft.com/en-us/library/aa332365(VS.71).aspx "The thread is not guaranteed to abort immediately, or at all."
Jeffrey C
@Chirs, in terms of [MTAThread] issue, would WaitAll work when using WCF?
Jeffrey C
@Jeffrey WCF shouldn't be an issue if you're running a service (which are MTA by default) unless WCF does something I don't know about with the winapi and message pumps. For the Thread abort issue: "*This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure*" - are you doing this at all? WaitAll and WaitHandles being Mutexes will throw an 'AbandonedMutexException'
Chris S
@Chris, I am not doing any thread abortion which I am trying to figure out. The code I am using is from this question which I asked earlier. http://stackoverflow.com/questions/2317691 But I think I should try abort all the remaining tasks once WaitHandle moves on.
Jeffrey C