c#中进程的StackOverflowException
本文关键字:StackOverflowException 进程 | 更新日期: 2023-09-27 17:53:07
我有一个进程,它在一个控制台应用程序中运行,它永远运行。
几天后,应用程序崩溃与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。出于好奇收集一下,看看它是否起了作用。
我不是在做Windows Service
在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
}
}