How to kill a process without getting a “process has exited” exception?

后端 未结 2 1755
礼貌的吻别
礼貌的吻别 2020-12-04 01:45

I use Process.Kill() to kill a process. Like this:

if( !process.WaitForExit( 5000 ) ) {
   process.Kill();
}

and sometimes the

2条回答
  •  [愿得一人]
    2020-12-04 02:09

    You need to use HasExited:

    if(!process.WaitForExit(5000)) 
    {
       if (!process.HasExited)
       {
           process.Kill();
       }
    }
    

    See here:

    http://msdn.microsoft.com/en-us/library/system.diagnostics.process.hasexited.aspx

提交回复
热议问题