使用 process.waitforexit() 时线程被中止错误
我下面的代码是从 while 循环调用的,因此它连续执行多次。有时,但并非总是如此,我最终会在 p.WaitforExit() 上收到线程被中止错误。有人对此有任何见解吗?我应该在 p.WaitForExit 之后调用 p.Close() 吗?
string outputFileName = Path.Combine(Path.GetDirectoryName(recordingFileName), Guid.NewGuid() + ".mpg");
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegfileName);
startInfo.Arguments = "-i \"" + recordingFileName + "\" -r 30 -sameq \"" + outputFileName + "\"";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(startInfo);
p.WaitForExit();
if (p.ExitCode != 0)
{
throw new Exception("exited with an exit code of " + p.ExitCode.ToString(CultureInfo.InvariantCulture) + ".");
}
return outputFileName;
Thread was being aborted. mscorlib at System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext) at System.Diagnostics.Process.WaitForExit(Int32 milliseconds) at System.Diagnostics.Process.WaitForExit()
I have code below that is getting called from a while loop so it's executing multiple times in a row. Sometimes, but not always, I end up getting a thread was being aborted error on the p.WaitforExit(). Does anyone have any insight into this? should I be calling p.Close() after p.WaitForExit?
string outputFileName = Path.Combine(Path.GetDirectoryName(recordingFileName), Guid.NewGuid() + ".mpg");
ProcessStartInfo startInfo = new ProcessStartInfo(ffmpegfileName);
startInfo.Arguments = "-i \"" + recordingFileName + "\" -r 30 -sameq \"" + outputFileName + "\"";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(startInfo);
p.WaitForExit();
if (p.ExitCode != 0)
{
throw new Exception("exited with an exit code of " + p.ExitCode.ToString(CultureInfo.InvariantCulture) + ".");
}
return outputFileName;
Thread was being aborted. mscorlib at System.Threading.WaitHandle.WaitOneNative(SafeWaitHandle waitHandle, UInt32 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext) at System.Threading.WaitHandle.WaitOne(Int64 timeout, Boolean exitContext) at System.Diagnostics.Process.WaitForExit(Int32 milliseconds) at System.Diagnostics.Process.WaitForExit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从 ASP.NET 页面调用它,您看到的异常很可能是您在页面请求上遇到执行超时并且正在中止的结果。错误出现在 p.WaitforExit() 行上,因为这是您的 ASP.NET 页面在等待您开始返回的进程时所在的位置。您可以在 ASP.NET 应用程序的 Web 配置文件中更改默认执行超时(2.0 中为 110 秒)。
或仅通过该页面的代码:
http://msdn.microsoft .com/en-us/library/e1f13641.aspx
If you are calling it from an ASP.NET page the Exception you are seeing is most likely a result of you hitting the Execution timeout on the Request for the page and it is being aborted. The error is on the p.WaitforExit() line because that is where your ASP.NET page was while it was waiting for the Process you started to return. You can change the default execution timeout (110 seconds in 2.0) in the web config file for your ASP.NET app.
or via code in the page for that page only:
http://msdn.microsoft.com/en-us/library/e1f13641.aspx