我有一个被调用的方法,虽然我希望在方法完成后显示消息框(现在在调用方法后直接显示消息框):
if (Check == true)
{
StartConvIpod();
}
else
{
}
MessageBox.Show("Operation Successful!");
StartConvIpod:
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();
}
答案 0 :(得分:11)
你想要添加这个:
p.Start();
p.WaitForExit(); // or p.WaitForExit(Timeout-Period-In-Milliseconds);
答案 1 :(得分:7)
在代码末尾使用此代码:
p.WaitForExit();
不要忘记检查其返回值以确保它确实成功,但是:
if(p.ExitCode == 0) { // Or whatever return code you're expecting
//...
}
答案 2 :(得分:3)
你有几个选择。在StartConvIpod
中,您可以在p.WaitForExit()
p.Start();
这样可行,但可能会阻止你的UI线程(让你的应用程序显然被冻结)。相反,我会将您的UI更改为某种“工作”状态,例如禁用“开始转换”按钮,并将标签设置为“转换”(仅作为示例)。然后我会在p.Exited
事件上注册并退出您的流程。引发事件后,您可以通知UI转换完成并检查过程中的退出代码。
答案 3 :(得分:0)
根据MSDN Documentation for the Process Exit Event使用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);
}