windows 等待外部进程完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6779791/
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
Waiting until an external process has been completed
提问by Adam Jones
I have a method that is called, although I would like the message box to be shown after the method has been completed (right now the message box is shown straight after the method is called):
我有一个被调用的方法,虽然我希望在方法完成后显示消息框(现在消息框在方法被调用后直接显示):
if (Check == true)
{
StartConvIpod();
}
else
{
}
MessageBox.Show("Operation Successful!");
StartConvIpod:
启动ConvIpod:
private void StartConvIpod()
{
string res = Directory.EnumerateFiles("dump").
OrderBy(x => File.GetCreationTime(x)).Last();
string sub = res.Substring(5);
string sub2 = sub.Substring(0, sub.Length - 4);
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = "-i " + sub + " -f mp4 -vcodec mpeg4 -b 700k -aspect 4:3 -r 23.98 -s 320x240 -acodec ac3 -ar 48000 iPodConversions\" + sub2 + ".mp4";
p.Start();
}
回答by Jason Down
You'll want to add this:
你会想要添加这个:
p.Start();
p.WaitForExit(); // or p.WaitForExit(Timeout-Period-In-Milliseconds);
回答by doctorless
Use this at the end of your code:
在代码末尾使用它:
p.WaitForExit();
Don't forget to check its return value to make sure it actually was successful, though:
不过,不要忘记检查它的返回值以确保它确实成功了:
if(p.ExitCode == 0) { // Or whatever return code you're expecting
//...
}
回答by vcsjones
You have a couple of options. In StartConvIpod
, you can put p.WaitForExit()
after p.Start();
你有几个选择。在StartConvIpod
,你可以把p.WaitForExit()
后p.Start();
That'll work, but will probably block your UI Thread (make it appears your application is frozen). Instead, I'd change your UI to some sort of "working" state, such as disabling the "Start Conversion" button, and set a label to "Converting" (just as an example). Then I'd register on the p.Exited
event and when your process is exited. When the event is raised, you can notify the UI your conversion is complete and check the exit code from the process.
这会起作用,但可能会阻塞您的 UI 线程(使其看起来您的应用程序已冻结)。相反,我会将您的 UI 更改为某种“工作”状态,例如禁用“开始转换”按钮,并将标签设置为“正在转换”(仅作为示例)。然后我会在p.Exited
事件上注册,并在您的进程退出时注册。引发事件时,您可以通知 UI 您的转换已完成并检查流程的退出代码。
回答by Jeremy Thompson
Use the Process.Exited Event as per the MSDN Documentation for the Process Exit Eventand poll for 30 seconds till the Exited event fires and check the ExitCode.
根据进程退出事件的MSDN 文档使用 Process.Exited 事件并轮询 30 秒,直到 Exited 事件触发并检查 ExitCode。
private Process myProcess = new Process();
private int elapsedTime;
private bool eventHandled;
public void RunFfmpeg(string arguments)
{
elapsedTime = 0;
eventHandled = false;
try
{
myProcess.StartInfo.FileName = "ffmpeg.exe";
myProcess.StartInfo.Arguments = arguments;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred trying to print \"{0}\":" + "\n" + ex.Message, fileName);
return;
}
// Wait for Exited event, but not more than 30 seconds.
const int SLEEP_AMOUNT = 100;
while (!eventHandled)
{
elapsedTime += SLEEP_AMOUNT;
if (elapsedTime > 30000)
{
break;
}
Thread.Sleep(SLEEP_AMOUNT);
}
}
private void myProcess_Exited(object sender, System.EventArgs e)
{
eventHandled = true;
Console.WriteLine("Exit time: {0}\r\n" +
"Exit code: {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
}