If I start a IronPyThon Engine in a C# thread, the python script will start a number of threads. However, when I kill the C# thread, all of the threads in the python script are still running. How can I kill all of the threads in the Python script?
-
2please show your code - how do you start the engine and how do you try to kill it – MikroDel Jun 07 '13 at 06:52
-
do you really want to kill **all** the threads? Then just exit the process ;) – quetzalcoatl Jun 11 '13 at 17:38
-
How do you "kill" the thread? Using `Thread.Kill` method is generally not advisable. Search SO for reasons (e.g. [here](http://stackoverflow.com/q/5034538/21567)). – Christian.K Jun 11 '13 at 17:49
-
1@olidev - why do you ignore the comments? – MikroDel Jun 12 '13 at 07:28
4 Answers
The simple Idea will be to start it as a Process
,
after you kill the Process
every Thread
of it will also be killed.

- 6,705
- 7
- 39
- 74
-
2+1: without knowing what the IronPython interpreter is actual doing, or not doing, when being forcefully taken out of existence, that is most likely the most robust way. Maybe running it in a separate appdomain could also be sufficient, but hard to tell. – Christian.K Jun 11 '13 at 17:54
If you're sure you want to kill them, what you do is walk the process list and check each process's parent ID. If the parent ID is the same as the ID of the IronPython process that you started, then you kill it. This will kill all of the threads associated with that process. Something like this, where processes is the full array of Processes and parent is the IronPython process that you started:
private static void killProcessAndChildProcesses(Process[] processes, Process parent)
{
foreach (Process p in processes)
{
if (p.GetParentProcessId() == parent.Id)
{
p.Kill();
}
}
parent.Kill()
}
If you're concerned about the IP child processes spawning processes of their own, you will need to convert this into a recursive solution.

- 23,435
- 23
- 108
- 157
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.

- 3,829
- 4
- 26
- 39
You can certainty do it in the reverse way. Since I don't know python I'll share the idea only.
- Set a named event in the system (you are in windows, its really simple in c#, Search for
Mutex
) - share the name of the named event with your python main thread
- In the main python thread set a termination flag, accessible by worker-python-threads
- Set worker-python-threads to monitor this flag occasionaly
- In the main python thread also, begin an infinite loop like below
- Set the named event in the c# thread whenever you want them to die
Pseudocode:
while (Not(isNamedEventSet) && otherThreadsStillBusy)
{
// Do some magic to check the status of the event
isNamedEventSet = ...
// rest for a while,
// there is nothing better than a nap while other threads do all the hard work
Sleep(100)
}
I've done similar scenarios between c#/c++ and it has been a success in all cases till now. I hope it fits python though..

- 8,951
- 10
- 69
- 111