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

时间:2010-01-08 14:37:52

标签: c# asp.net multithreading process

下面的代码是从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()

1 个答案:

答案 0 :(得分:3)

如果从ASP.NET页面调用它,您看到的Exception很可能是因为您在页面请求上达到执行超时而中止了。错误发生在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