c# - StackOverflowException 与 C# 中的进程

标签 c# ffmpeg

我有一个进程,它在控制台应用程序中运行。它永远运行。

几天后,应用程序因 StackOverflowException 而崩溃。

该应用程序的本质是我使用 FFMpeg.exe 启动进程并创建视频流的截图。它工作得很好,但当时只有几天。

我很确定这与 FFMpeg 或一些内部流程的处理有关。

这是代码

using ( Process ffmpegProcess = new Process() ) {

    //arguments for running ffmpeg
    ffmpegProcess.StartInfo.UseShellExecute = false;
    ffmpegProcess.StartInfo.CreateNoWindow = true;
    ffmpegProcess.StartInfo.RedirectStandardOutput = true;

    //specific for our screenshots
    ffmpegProcess.StartInfo.FileName = string.Concat( Environment.CurrentDirectory, Path.DirectorySeparatorChar, ffmpegProgramName );

    try {
        //todo: log this stopwatch somewhere perhaps
        processWatch.Start();

        //set arguments every time we want to create a new screen shot
        ffmpegProcess.StartInfo.Arguments = string.Format( @"-y -i {0}{1} -threads 0 -ss 00:00:01.000 -f image2 -s 620x349 -vframes 1 ../../web/{2}.jpg", server, streamPath, slug );
        ffmpegProcess.Start();
        ffmpegProcess.WaitForExit( 500 );

        Console.WriteLine( slug );
        Console.WriteLine( processWatch.Elapsed );

        processWatch.Reset();
        runCount++;
        cacheIndexer++;

        //lets see how many spins we've had!
        Console.WriteLine( string.Format( "SERVER CACHE INDEX : {0}", cacheIndexer ) );
        Console.WriteLine( string.Format( "RUN : {0}", runCount ) );
        Console.WriteLine( Environment.NewLine );

    } catch ( Exception ex ) {
        //Console.WriteLine( "Ex " + ex );
    }
}

循环看起来像这样。
    public void RecurseTask() {
        /*
        You can try one of these, but you will se CPU usage go crazy and perhaps concurrency errors due IO blocking

        Parallel.ForEach( _videoStreamSlugs, ( e ) => _videoStreamScreenShots.GrabScreenShot( e ) );

        foreach ( var slug in _videoStreamSlugs ) {
            Task.Run( () => _videoStreamScreenShots.GrabScreenShot( slug ) );
        }
        */

        //we want to grab screen shots for every slug in out slug list!
        foreach ( var slug in _videoStreamSlugs ) {
            _videoStreamScreenShots.GrabScreenShot( slug );
        }

        //sleep for a little while
        Thread.Sleep( _recurseInterval );

        //A heavy clean up!
        //We do this, trying to avoid a stackoverflow excecption in the recursive method
        //Please inspect this if problems arise
        GC.Collect();

        //lets grab over again
        RecurseTask();
    }

出于好奇,我添加了一个 GC.Collect 以查看它是否有所作为。

我没有做 Windows 服务。

最佳答案

在 RecurseTask 内部你总是调用 RecurseTask,显然,长时间运行它会抛出 StackOverflowException,你可以尝试更改为

public void RecurseTask() {
    while(true)
    {
        /*
        You can try one of these, but you will se CPU usage go crazy and perhaps concurrency errors due IO blocking

        Parallel.ForEach( _videoStreamSlugs, ( e ) => _videoStreamScreenShots.GrabScreenShot( e ) );

        foreach ( var slug in _videoStreamSlugs ) {
            Task.Run( () => _videoStreamScreenShots.GrabScreenShot( slug ) );
        }
        */

        //we want to grab screen shots for every slug in out slug list!
        foreach ( var slug in _videoStreamSlugs ) {
            _videoStreamScreenShots.GrabScreenShot( slug );
        }

        //sleep for a little while
        Thread.Sleep( _recurseInterval );

        //A heavy clean up!
        //We do this, trying to avoid a stackoverflow excecption in the recursive method
        //Please inspect this if problems arise
        //GC.Collect(); Not needed

        //lets grab over again
    }
}

关于c# - StackOverflowException 与 C# 中的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30637452/

相关文章:

c# - 派生类能否破坏基类的实现

c# - 如何在asp.net应用程序中使用命名空间?

c# - dotnet pack项目引用

c# - 在 Visual Studio 中运行 PowerShell 脚本时无法获取 Active Directory 终端服务属性

linux - 使用 pkg-config 找不到错误 : dav1d >= 0. 2.1

c++ - FFMPEG I/O 的自定义读取功能

c# - 最大化另一个正在运行的程序的窗口

documentation - 将库的文档添加到手册页

c# - 在 C# 中完成处理后如何停止 ffmpeg 进程?

PHP popen() 或 proc_open() 来存储/设置 pid,这样我就可以在我启动的窗口中关闭进程