Wait till process is no longer running OR until timeout has passed, whichever happens first

后端 未结 2 888
半阙折子戏
半阙折子戏 2021-02-12 07:51

I want to kill a process running on the machine using taskkill if they\'re still running after X seconds (a windows service was stopped but it takes time for processes to diss

相关标签:
2条回答
  • 2021-02-12 08:32

    In fact, you can use Process.WaitForExit().

    Simply get the process to wait for via

    Process p = Process.GetProcessById(iYourID);
    

    And then call

    if(!p.WaitForExit(iYourInterval))
    { 
        p.Kill();
    }
    

    You can also get processes via their name by

    Process.GetProcessesByName(strYourProcessName);
    
    0 讨论(0)
  • 2021-02-12 08:49

    You could call Process.WaitForExit, passing the appropriate timeout. This way you won't need to use your own check to see whether the process is still running.

    0 讨论(0)
提交回复
热议问题