Cancel on ThreadPool queued executions?
-
Smart Thread Pool[^] supports cancelation of queued items.
Harvey Saayman - South Africa Software Developer .Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111 -
The standard ThreadPool holds threads for you to use; you never get full control over those threads, so there are a lot of managerial operations you can't perform on them, such as changing their priority, or aborting them. What you can do is implement a co-operational abort, say a while loop where the outside changes the expression or variable that is going to be tested.
public void myThreadAction(...) {
while(!threadStop) {
... go on
}
}The net result most often is the "abort" is not instantaneous, the while loop may (and should) call some blocking method (some input/output operation, or a Thread.Sleep). BTW: that is also the way you should do it on normal threads, a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Smart Thread Pool[^] supports cancelation of queued items.
Harvey Saayman - South Africa Software Developer .Net, C#, SQL
you.suck = (you.Occupation == jobTitles.Programmer && you.Passion != Programming)
1000100 1101111 1100101 1110011 100000 1110100 1101000 1101001 1110011 100000 1101101 1100101 1100001 1101110 100000 1101001 1101101 100000 1100001 100000 1100111 1100101 1100101 1101011 111111 -
Glad to be of service :)
-
The standard ThreadPool holds threads for you to use; you never get full control over those threads, so there are a lot of managerial operations you can't perform on them, such as changing their priority, or aborting them. What you can do is implement a co-operational abort, say a while loop where the outside changes the expression or variable that is going to be tested.
public void myThreadAction(...) {
while(!threadStop) {
... go on
}
}The net result most often is the "abort" is not instantaneous, the while loop may (and should) call some blocking method (some input/output operation, or a Thread.Sleep). BTW: that is also the way you should do it on normal threads, a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Luc Pattyn wrote:
a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
Sorry any elaboration on this...? I can't see my calculation threads has any "State" to it... and my guess is for all practical reason I can throw an exception in the thread just to kill it. Am I missing something, Abort vs Interrupt?
dev
-
Luc Pattyn wrote:
a hard abort is to be avoided as one never knows in what state objects (such as locks, unmanaged resources) will be at the moment of the abort.
Sorry any elaboration on this...? I can't see my calculation threads has any "State" to it... and my guess is for all practical reason I can throw an exception in the thread just to kill it. Am I missing something, Abort vs Interrupt?
dev
you'll have state whenever some I/O is included, or something gets locked (e.g. a shared results queue), etc. Here is an excellent article on threading: http://www.albahari.com/threading/[^] My conclusion is: choose the co-operative way whenever you can. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.