windows 等待进程完成,然后显示消息 (C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6723720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait for process to finish and then display message (C#)
提问by James
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?
我希望能够观看一个进程直到它终止,并且一旦不存在显示一条消息,如何实现?
回答by Olipro
Create/Attach to the process and then either use WaitForExit()
to block until it has exited, or use the OnExited
Event if you don't wish your application to block while it's waiting for the app to exit.
创建/附加到进程,然后使用WaitForExit()
阻塞直到它退出,或者OnExited
如果您不希望应用程序在等待应用程序退出时阻塞,则使用事件。
I heartily recommend reviewing the documentation for Process
- right here
我衷心建议您查看文档Process
-就在这里
回答by Cody Gray
The .NET Framework has built in support for this. You need to use the Process.Start
methodto 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.
.NET Framework 内置了对此的支持。您需要使用该Process.Start
方法启动进程,然后调用该WaitForExit
方法,该方法将阻止您的应用程序的执行,直到您启动的进程完成并关闭。
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#
相关知识库文章:如何使用 Visual C# 等待带壳的应用程序完成
回答by 1' OR 1 --
I think this is what you want to do:
我认为这就是你想要做的:
System.Diagnostics.Process process=new System.Diagnostics.Process();
process.StartInfo.FileName = "process.exe";
process.Start();
process.WaitForExit();
//process ended
MessageBox.Show("Process terminated");