简体   繁体   English

C#中具有Process的StackOverflowException

[英]StackOverflowException with Process in C#

I have a process, which runs in a console app. 我有一个进程,该进程在控制台应用程序中运行。 It runs forever. 它永远运行。

After a couple of days the app crashes with a StackOverflowException. 几天后,该应用程序崩溃并出现StackOverflowException。

The essence of the app is where I spin up a Process with FFMpeg.exe and creates a sceenshot of a video stream. 该应用程序的本质是我使用FFMpeg.exe启动一个Process并创建视频流的画面。 It works very good but only for a few days at the time. 它的效果非常好,但一次只能使用几天。

I am pretty sure it has to do with the disposal of the FFMpeg or some internal Process stuff. 我很确定这与处理FFMpeg或一些内部Process无关。

Here is the code 这是代码

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 );
    }
}

The loop looks like this. 循环看起来像这样。

    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();
    }

I added a GC.Collect out of curiosity to see if it made a difference. 我出于好奇而添加了GC.Collect,以查看它是否有所作为。

I am not doing a Windows Service. 我没有做Windows服务。

Inside RecurseTask you always call RecurseTask, obviously, when long running it will throw a StackOverflowException, you can try to change to 在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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM