1、区别
public static async void Execute(string para, string ffmpegPath, string timestr, string Id, string targetUrl) { await Task.Run(() => { CreTimeStr = timestr; rowId = Id; compPath = targetUrl; Process p = new Process(); p.StartInfo.FileName = ffmpegPath; p.StartInfo.Arguments = para; //执行参数 p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中 p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived); p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); using (p) { p.Start(); p.BeginErrorReadLine();//开始异步读取 p.WaitForExit();//阻塞等待进程结束 p.Close();//关闭进程 } }); }
上面的方式 async 方法必须配合 await ,导致 Task.Run 中的
一直运行,哪怕
提前return也无效。