System.AccessViolationException:.NET运行时错误c#工作线程

时间:2017-11-13 07:28:37

标签: c# multithreading video-streaming dispose access-violation

我的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来检测视频中的盘子。但是当我点击按钮几次时,我收到此错误:< / p>

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

enter image description here

1 个答案:

答案 0 :(得分:2)

首先你打开视频阅读器有问题,我认为你的解决方案的平台目标存在问题,尝试使其成为32位,一些使用C ++本机dll的库可能是32位,并且不会#39;与AnyCPU合作。

其次您正在从主线程以外的线程更改picturebox的背景图像:

在更改picturebox图片时尝试调用Invoke:

this.Invoke(new Action(()=>  pictureRTSP.BackgroundImage = currentFrame ));