System.AccessViolationException: .NET 运行时错误 c# 工作线程
System.AccessViolationException : .NET Runtime Error c# worker thread
我的 windows 表单中有一个图片框,它使用工作线程呈现来自我的相机的流,如您所见:
VideoFileReader reader = new VideoFileReader();
Thread Proceso1;
Proceso1 = new Thread(new ThreadStart(updateui));
public void updateui()
{
try
{
reader.Open(RTSPAddress);
while (true)
{
var previousFrame = pictureRTSP.BackgroundImage;
Bitmap currentFrame = reader.ReadVideoFrame();
pictureRTSP.BackgroundImage = currentFrame;
if (previousFrame != null)
this.Invoke(new MethodInvoker(delegate () {
previousFrame.Dispose();
}));
}
reader.Close();
}
catch(ArgumentException ee)
{
//Text = ee.ToString();
}
}
由于内存使用,我处理了前一帧。在我的表单中,我有一个按钮,当我点击按钮几次时,我得到了使用 pictureRTSP.BackgroundImage
检测 video.But 中的盘子这个错误:
Application: nMCR.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at <Module>.av_read_frame(libffmpeg.AVFormatContext*, libffmpeg.AVPacket*)
at Accord.Video.FFMPEG.VideoFileReader.readVideoFrame(Int32, System.Drawing.Imaging.BitmapData)
at Accord.Video.FFMPEG.VideoFileReader.ReadVideoFrame()
at nMCR.form.MainForm.updateui()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
我的按钮代码:
BitmapImage bmImage = null;
if (IsRTSP)
{
//FinalImage = new Bitmap(_snapshotHandler.TakeSnapshot().ToImage());
FinalImage = new Bitmap(pictureRTSP.BackgroundImage);
//Bitmap img = (Bitmap)Image.FromStream(FinalImage);
bmImage = new BitmapImage();
using (MemoryStream memStream2 = new MemoryStream())
{
FinalImage.Save(memStream2, System.Drawing.Imaging.ImageFormat.Png);
memStream2.Position = 0;
bmImage.BeginInit();
bmImage.CacheOption = BitmapCacheOption.OnLoad;
bmImage.UriSource = null;
bmImage.StreamSource = memStream2;
bmImage.EndInit();
}
首先你在打开视频时遇到了问题reader,我认为你的解决方案的平台目标有问题,尝试将其设为 32 位,一些在内部使用 C++ 原生 dll 的库可能是 32 位的,并且不适用于 AnyCPU。
其次,您正在从主线程以外的线程更改 picturebox
的背景图像:
尝试在更改 picturebox
图像时调用 Invoke :
this.Invoke(new Action(()=> pictureRTSP.BackgroundImage = currentFrame ));
我的 windows 表单中有一个图片框,它使用工作线程呈现来自我的相机的流,如您所见:
VideoFileReader reader = new VideoFileReader();
Thread Proceso1;
Proceso1 = new Thread(new ThreadStart(updateui));
public void updateui()
{
try
{
reader.Open(RTSPAddress);
while (true)
{
var previousFrame = pictureRTSP.BackgroundImage;
Bitmap currentFrame = reader.ReadVideoFrame();
pictureRTSP.BackgroundImage = currentFrame;
if (previousFrame != null)
this.Invoke(new MethodInvoker(delegate () {
previousFrame.Dispose();
}));
}
reader.Close();
}
catch(ArgumentException ee)
{
//Text = ee.ToString();
}
}
由于内存使用,我处理了前一帧。在我的表单中,我有一个按钮,当我点击按钮几次时,我得到了使用 pictureRTSP.BackgroundImage
检测 video.But 中的盘子这个错误:
Application: nMCR.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at <Module>.av_read_frame(libffmpeg.AVFormatContext*, libffmpeg.AVPacket*)
at Accord.Video.FFMPEG.VideoFileReader.readVideoFrame(Int32, System.Drawing.Imaging.BitmapData)
at Accord.Video.FFMPEG.VideoFileReader.ReadVideoFrame()
at nMCR.form.MainForm.updateui()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
我的按钮代码:
BitmapImage bmImage = null;
if (IsRTSP)
{
//FinalImage = new Bitmap(_snapshotHandler.TakeSnapshot().ToImage());
FinalImage = new Bitmap(pictureRTSP.BackgroundImage);
//Bitmap img = (Bitmap)Image.FromStream(FinalImage);
bmImage = new BitmapImage();
using (MemoryStream memStream2 = new MemoryStream())
{
FinalImage.Save(memStream2, System.Drawing.Imaging.ImageFormat.Png);
memStream2.Position = 0;
bmImage.BeginInit();
bmImage.CacheOption = BitmapCacheOption.OnLoad;
bmImage.UriSource = null;
bmImage.StreamSource = memStream2;
bmImage.EndInit();
}
首先你在打开视频时遇到了问题reader,我认为你的解决方案的平台目标有问题,尝试将其设为 32 位,一些在内部使用 C++ 原生 dll 的库可能是 32 位的,并且不适用于 AnyCPU。
其次,您正在从主线程以外的线程更改 picturebox
的背景图像:
尝试在更改 picturebox
图像时调用 Invoke :
this.Invoke(new Action(()=> pictureRTSP.BackgroundImage = currentFrame ));