1

I am just trying to make a simple program that starts, makes screenshot from my camera and closes. I am using Aforge.net but my camera does not have any SnapshotCapabilities, so I have to use VideoCapabilities. I have this simple code:

namespace Make_picture
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice cam;
        private Image img;
        private int ind = 1;

        public Form1()
        {
            InitializeComponent();
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        }

        private void CameraStart()
        {
            cam = new VideoCaptureDevice(videoDevices[ind].MonikerString);
            cam.NewFrame += new NewFrameEventHandler(cam_NewFrame);
            cam.Start();
        }


        private void CameraStop()
        {
            if (cam != null)
            {
                if (cam.IsRunning)
                {
                    cam.Stop();
                }
            }
        }

        void cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            img = (Bitmap)eventArgs.Frame.Clone();
            img.Save("image.png", System.Drawing.Imaging.ImageFormat.Png);

            cam.NewFrame -= new NewFrameEventHandler(cam_NewFrame);
            this.Close();

            //this.Invoke((MethodInvoker)delegate() { this.Close(); });
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            CameraStart();
        }

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

I have issue to close program after first new frame is fired. It says that I cannot close it on this thread because form was created on another (Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on). Searching stack exchange I have tried also this:

this.Invoke((MethodInvoker)delegate() { this.Close(); });

but it does not work either (no exception, but form keeps hanging and I cannot do nothing with him). Could you help me with this issue? Really thanks.

Yann

PS: I am not a programmer, so this could be done a lot differently I guess. But I think this could work If I knew how to kill the form :)

xYann00
  • 48
  • 6
  • There's nothing wrong with your `Invoke()` call to close the form. You could try calling `CameraStop()` before attempting to close the form. – Idle_Mind Oct 05 '15 at 21:12
  • [Program wont close and aforge camera keeps running, threading trouble](http://stackoverflow.com/a/13802331/3110834) – Reza Aghaei Oct 05 '15 at 21:36
  • Standard deadlock, always a problem with Invoke(). The camera software isn't going to be very happy about you calling Stop() while its NewFrame event has not finished executing yet. Use BeginInvoke() instead of Invoke(). – Hans Passant Oct 06 '15 at 00:57
  • BeginInvoke() worked, thanks for help :) – xYann00 Dec 06 '15 at 13:21

0 Answers0