Can I Hard Kill a Thread?
-
My situation is this: I'm writing an app that needs to execute a separate program (we'll call it aaa.exe) and monitor its output so that my app can display the status of aaa.exe to the user. To do this I've started aaa.exe using a
Process
and have redirected the STDOUT stream to aStreamReader
so that I can monitor the status messages that it spits out and use that information to update a status variable. Basically, what I have to monitor the stream is this:string line = "";
while (line = stdout.ReadLine()) != null)
{
// do stuff
}and then I parse
line
to determine the status of the spawned application. To prevent thiswhile
loop from locking up my GUI I've moved this code to another thread, so that the status variable will continually be updated by one thread and the GUI can intermittently poll that variable from it's own thread and remain responsive to the user. Under normal circumstances, this works well. Thewhile
loop continues until aaa.exe stops sending data to STDOUT and then the thread monitoring it exits happily and all is well. The problem arises when things don't go well in aaa.exe. It is possible under worst case scenario conditions (so this happens frequently while I'm testing) that the spawned application locks up. When this happens, my code freezes execution on the function callstdout.ReadLine()
, and I now have a deadlocked thread. The most direct way to end the rogue thread is to hard kill the process for aaa.exe (usingProcess.Kill()
), but my boss has just informed that this is not an acceptable solution as it causes horrendous side effects to the operating environment. UsingThread.Abort
has no apparent effect, it seems to wait for the next instruction to finish before doing anything to the thread which means nothing if the thread is suspended in a function call. So my question is this: is there some other way to end the Thread without killing the instance of aaa.exe, or more generally, is there some way to kill a non-responsive Thread in my application without doing a hard kill on the application itself? -
My situation is this: I'm writing an app that needs to execute a separate program (we'll call it aaa.exe) and monitor its output so that my app can display the status of aaa.exe to the user. To do this I've started aaa.exe using a
Process
and have redirected the STDOUT stream to aStreamReader
so that I can monitor the status messages that it spits out and use that information to update a status variable. Basically, what I have to monitor the stream is this:string line = "";
while (line = stdout.ReadLine()) != null)
{
// do stuff
}and then I parse
line
to determine the status of the spawned application. To prevent thiswhile
loop from locking up my GUI I've moved this code to another thread, so that the status variable will continually be updated by one thread and the GUI can intermittently poll that variable from it's own thread and remain responsive to the user. Under normal circumstances, this works well. Thewhile
loop continues until aaa.exe stops sending data to STDOUT and then the thread monitoring it exits happily and all is well. The problem arises when things don't go well in aaa.exe. It is possible under worst case scenario conditions (so this happens frequently while I'm testing) that the spawned application locks up. When this happens, my code freezes execution on the function callstdout.ReadLine()
, and I now have a deadlocked thread. The most direct way to end the rogue thread is to hard kill the process for aaa.exe (usingProcess.Kill()
), but my boss has just informed that this is not an acceptable solution as it causes horrendous side effects to the operating environment. UsingThread.Abort
has no apparent effect, it seems to wait for the next instruction to finish before doing anything to the thread which means nothing if the thread is suspended in a function call. So my question is this: is there some other way to end the Thread without killing the instance of aaa.exe, or more generally, is there some way to kill a non-responsive Thread in my application without doing a hard kill on the application itself?Instead of having a dedicated thread that reads the standard output in a while loop, is it possible to just do something like:
Process proc = ...;
proc.OutputDataReceived += OutputReceivedHandler;...
void OutputReceivedHandler(object sender, DataReceivedEventArgs e)
{
// do stuff
}That way, you're in a message-based system where you don't need to spawn/rendezvous/kill other threads.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Virginia Tech Shootings, Guns, and Politics The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Instead of having a dedicated thread that reads the standard output in a while loop, is it possible to just do something like:
Process proc = ...;
proc.OutputDataReceived += OutputReceivedHandler;...
void OutputReceivedHandler(object sender, DataReceivedEventArgs e)
{
// do stuff
}That way, you're in a message-based system where you don't need to spawn/rendezvous/kill other threads.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Virginia Tech Shootings, Guns, and Politics The apostle Paul, modernly speaking: Epistles of Paul Judah Himango