使用 process.waitforexit() 时线程被中止错误

发布于 2024-08-16 21:38:55 字数 1154 浏览 2 评论 0原文

我下面的代码是从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹉夏雨初晴づ 2024-08-23 21:38:55

如果您从 ASP.NET 页面调用它,您看到的异常很可能是您在页面请求上遇到执行超时并且正在中止的结果。错误出现在 p.WaitforExit() 行上,因为这是您的 ASP.NET 页面在等待您开始返回的进程时所在的位置。您可以在 ASP.NET 应用程序的 Web 配置文件中更改默认执行超时(2.0 中为 110 秒)。

<httpRuntime  executionTimeout = "600" >

或仅通过该页面的代码:

  HttpContext.Current.Server.ScriptTimeout

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.

<httpRuntime  executionTimeout = "600" >

or via code in the page for that page only:

  HttpContext.Current.Server.ScriptTimeout

http://msdn.microsoft.com/en-us/library/e1f13641.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文