I have a thread which contains execution of an IronPython script. For some reason I may need to stop this thread at any time, including script execution. How to achieve this? The first idea is Thread.Abort()
, but it's known to be evil...
Well from you question and subsequent comments I can suggest you two options, with some additional "warnings":
If your thread loops executing something on each iteration, you can set a volatile boolean flag such that it exits after finishing the current iteration (pseudocode because I'm not familiar with python):
while shouldExit = false // do stuff
Then just set the flag to
true
when you want the thread to stop and it will stop the next time it checks the condition.If you cannot wait for the iteration to finish and need to stop it immediately, you could go for
Thread.Abort
, but make absolutely sure that there is no way you can leave open file handles, sockets, locks or anything else like this in an inconsistent state.
Thread.Abort()
, because everywhere people write something like "NEVER USE IT!!!"))). But now I use it in the program - ho handles may stay opened. –
Deadhead try { // do work - no try/catches here } catch(ThreadAbortException) { // log exit }
will be stopped after calling Abort()
on it? –
Deadhead try
, it should work properly. –
Gaul What is a safe way to stop a running thread?
Put the thread in its own process. When you want it to stop, kill the process.
That is the only safe way to kill a thread. Aborting a thread can severely destabilize a process and lose user data. There's no way to avoid the "lose user data" scenario if you really, truly need to be able to kill a thread that could be doing anything. The only way to avoid destabilizing the process that is calling for the abort is to make them different processes entirely.
myprogram.exe
could start a new process running myprogram.exe
. –
Inadvisable Well from you question and subsequent comments I can suggest you two options, with some additional "warnings":
If your thread loops executing something on each iteration, you can set a volatile boolean flag such that it exits after finishing the current iteration (pseudocode because I'm not familiar with python):
while shouldExit = false // do stuff
Then just set the flag to
true
when you want the thread to stop and it will stop the next time it checks the condition.If you cannot wait for the iteration to finish and need to stop it immediately, you could go for
Thread.Abort
, but make absolutely sure that there is no way you can leave open file handles, sockets, locks or anything else like this in an inconsistent state.
Thread.Abort()
, because everywhere people write something like "NEVER USE IT!!!"))). But now I use it in the program - ho handles may stay opened. –
Deadhead try { // do work - no try/catches here } catch(ThreadAbortException) { // log exit }
will be stopped after calling Abort()
on it? –
Deadhead try
, it should work properly. –
Gaul © 2022 - 2024 — McMap. All rights reserved.
Thread.Abort()
is the way to go. The better solution is to modify your application's design. – NullityThread.Abort
not being ideal, but it's not that function that's evil, it's what it's doing. And since it's doing precisely what you want... – Nullity