简体   繁体   English

等待进程完成,然后显示消息(C#)

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

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?我希望能够观察一个进程直到它被终止,并且一旦不存在显示一条消息,这怎么能实现?

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的文档 -就在这里

The .NET Framework has built in support for this. .NET 框架内置了对此的支持。 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.您需要使用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# 等待外壳应用程序完成

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM