I know, I know, there are many questions and good answers on how to kill a process after x time in C#.
However after reading and writing the code to achieve this task, I find that is impossible to kill a process after a X time. Due to my novice in C#, I find difficult to understand the cause of error and correct it.
The task I want to achieve is to kill a process after 40 minutes, that process is started by a button click. I'm writing to the console:
private void click_start(Object sender, RoutedEventArgs e) {
using(Process p = Process.Start(@"calc.exe");
{
try{
p.WaitForExit(5000);
if(!p.HasExited) {
p.kill();
Console.WriteLine("Kill process waitforexit is false");
} else {
Console.WriteLine("NO Kill, waitforexit is true");
}
}
catch (Exception ex) {
Messageox.Show(ex.Messaage);
}
}
Of all the answers in SO, this is the generalized accepted answer.
When running the code on VS 2015, the debugger gives me this error: "Can't process the request because the Process is finished." Also always get the same console line: No kill which indicates that WaitForExit is still true. This after waiting more than 10 minutes event the time to kill is set to less than one minute.
So I don't understand: I launched the process but when ordered to kill, it's yet finished?