Wait for process to finish and then display message (C#)

前端 未结 3 1659
梦毁少年i
梦毁少年i 2020-12-11 19:57

I would like to be able to watch a process until it is terminated, and once non existent display a message, how could this be achieved?

3条回答
  •  不知归路
    2020-12-11 20:36

    The .NET Framework has built in support for this. You need to use the Process.Start method to start the process, and then call the WaitForExit method, which will block execution of your application until the process you started has finished and closed.

    Sample code:

    // Start the process.
    Process proc = Process.Start("notepad.exe");  // TODO: NEVER hard-code strings!!!
    
    // Wait for the process to end.
    proc.WaitForExit();
    
    // Show your message box.
    MessageBox.Show("Process finished.");
    


    Related knowledge base article: How to wait for a shelled application to finish using Visual C#

提交回复
热议问题