This is not about terminating a system-process but killing "myself". I have several parallel theads, which CAN hang because of different reasons.
I already created a watchdog when a thread is taking too long:
TimerCallback timerDelegate = new TimerCallback(CheckProcessStatus);
System.Threading.Timer watchDogTimer = new Timer(timerDelegate, new ProcessHealth(plog), 1000 * 60, 1000 * 60);
try
{
// lots of code here
} finally
{
watchDogTimer.Dispose();
}
Watchdog:
public void CheckProcessStatus(Object timerState) {
ProcessHealth ph = (ProcessHealth)timerState;
System.writeLine(string.Format("process runs for {0} minutes!", ph.WaitingTime);
if (ph.WaitingTime>60) {
// KILL THE PROCESS
}
}
When "lots of code here" takes too long I want to terminate the thread no matter what state it is in. (at "Kill the process").
What would be the best approach?
Thread.CurrentThread.Interrupt()
OR
Thread.CurrentThread.Abort()?
Or are there even better approaches? (I cannot use "simple" mechanisms like boolean "stop"-variables as the "lots of code here" is VERY Dynamic calling other classes via reflection etc.
Does that even work? Or do I just kill the watchdog-thread, NOT the thread to be watched?