Aforge PictureBox throws "System.InvalidOperationException: 'The object is currently in use elsewhere.'"

103 Views Asked by At

Aforge PictureBox throws "System.InvalidOperationException: 'The object is currently in use elsewhere.'" when I resize or move the application window. In some situations it throws an Exception that indicates the use in several threads.

Exception

1

Main Code:

    public partial class MainForm : Form
    {
    
    //Global Var

        String[] serialLog = new String[300];
        SerialLogDisplay sld_form;

        FilterInfoCollection filterInfoCollection;
        VideoCaptureDevice videoCaptureDevice;

        //Form Method

        public MainForm()
        {
            InitializeComponent();
        }




    //Generated Methods

        private void btnCCamera_Click(object sender, EventArgs e)
        {
            if(cmbCameraS.SelectedIndex > 0) {
                disconnectVideoStream(false);
                videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cmbCameraS.SelectedIndex-1].MonikerString);
                videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
                videoCaptureDevice.Start();

                Add_Log("Camera \"" + filterInfoCollection[cmbCameraS.SelectedIndex-1].Name+ "\" Connected!");
            }
            else
            {
                disconnectVideoStream(true);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Camera System
            cmbCameraS.Items.Add("None");
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo filterInfo in filterInfoCollection)
            {
                cmbCameraS.Items.Add(filterInfo.Name);
            }
            cmbCameraS.SelectedIndex = 0;
            videoCaptureDevice = new VideoCaptureDevice();

            //Log
            shortLog.Parent = cameraDisplay;
            shortLog.BackColor = Color.Transparent;
            shortLog.Text = serialLog[0] + "\n" + serialLog[1] + "\n" + serialLog[2];


            //Loop
            Timer tmr = new Timer();
            tmr.Interval = 1000;
            tmr.Tick += Tmr_Tick;
            tmr.Start();


            //DisplayObjects
            MPpanel.Parent = cameraDisplay;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            disconnectVideoStream(false);
        }

        private void SLbtn_Click(object sender, EventArgs e)
        {
            if (sld_form == null)
            {
                sld_form = new SerialLogDisplay(this);
            }
            sld_form.update_log(serialLog);
            sld_form.Show();
            sld_form.Activate();
        }




        //Created Methods

        //Serial Log
        private void Tmr_Tick(object sender, EventArgs e)
        {
            String lm = "";
            for (int i = 4; i >= 0; i--)
            {
                if (serialLog[i] != null)
                {
                    lm += serialLog[i] + "\n";
                }
            }
            shortLog.Text = lm;
        }

        public void SerialLogDisplay_Close()
        {
            sld_form = null;
        }

        public void Add_Log(String text)
        {
            for (int i = serialLog.Length - 1; i > 0; i--)
            {
                serialLog[i] = serialLog[i - 1];
            }
            serialLog[0] = text;

            if(sld_form != null)
            {
                sld_form.update_log(serialLog);
            }
            
        }

        public void Serial_Command(String command)
        {
            Add_Log(command);
            //Send command to serial Port
        }



        //Camera Output
        private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            try
            {
                if (cameraDisplay.Image != null)
                {
                    cameraDisplay.Image.Dispose();
                }
                Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
                cameraDisplay.Image = bmp;
            }
            catch (System.InvalidOperationException ex)
            {
                Add_Log("Error: " + ex);
                disconnectVideoStream(true);
            }

            //GC.Collect();
        }

        private void disconnectVideoStream(bool showLog)
        {
            if (videoCaptureDevice.IsRunning == true)
            {
                if (showLog) { Add_Log("Disconnecting video stream..."); }
                videoCaptureDevice.SignalToStop();
                videoCaptureDevice.WaitForStop();
                videoCaptureDevice.Stop();
                cameraDisplay.Image = null;
            }

            if (showLog)
            {
                Add_Log("No camera connected!");
            }
            GC.Collect();
        }
    }

"CameraDysplay" -> PictureBox

I tried to put the Bitmap image as global, but it didn't work... I also tried blocking the application window size change, but the error remains...

I tried to use invoke and it worked, but it crashes when closing the application.

        private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Invoke(new Action(() => {
                try
                {
                    if (cameraDisplay.Image != null)
                    {
                        cameraDisplay.Image.Dispose();
                    }
                    Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
                    cameraDisplay.Image = bmp;
                }
                catch (System.InvalidOperationException ex)
                {
                    Add_Log("Error: " + ex);
                    disconnectVideoStream(true);
                }
            }));
            //GC.Collect();
        }
0

There are 0 best solutions below