Hi,
I'm using C#.net 3.0 to develop an application on digital watermarking.I'm using FrameGrabber class which I have found on this site.What the class is doing-it is getting the constituent frames in the Bitmap format (*.bmp) from the given video file.Now the problem is that there is no way to stop the function in the middle of execution i.e. if I want to stop the function from extracting further more frames I have to shutdown the application.Now I want to put a function on Button(STOP) such that when I press the button it will stop the function from further execution and all the frames so far extracted should remain in the destined folder.Can anybody please tell me how to do this.

This is the code for Button- Extact frames

private void button43_Click(object sender, EventArgs e)//to extract frame
{
string outPath = txtExtBitmap.Text;//path of destination folder where the extracted frames are kept
System.IO.Directory.CreateDirectory(outPath);

if (fg != null)
{
foreach (FrameGrabber.Frame f in fg)
{
using (f)
{
picBoxFrame.Image = (Bitmap)f.Image.Clone();
f.Image.Save(System.IO.Path.Combine(outPath, "frame" + f.FrameIndex + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);//save each frame in *.bmp format
Application.DoEvents();
}

if (fg == null)
{
return;
}
}
}

Thank You!!

Recommended Answers

All 4 Replies

You should create another thread for this method to run in, and suspend this thread at anytime you want.

yeah, ramy is right. a background worker would even do for something simple like this, it would even make it incredibility simple to add a progress bar. Plus built in cancel method.

Hi,
Thanks for your reply
I used this code but it is still showing error highlighting fThread.Interrupt() ---Object reference not set to an instance of an object.Now what changes should I do in order to make this code work

private void frameExtract()//function to extract frames
        {
            string outPath = txtExtBitmap.Text;
            System.IO.Directory.CreateDirectory(outPath);

            if (fg != null)
            {
                foreach (FrameGrabber.Frame f in fg)
                {
                    using (f)
                    {
                        picBoxFrame.Image = (Bitmap)f.Image.Clone();
                        f.Image.Save(System.IO.Path.Combine(outPath, "frame" + f.FrameIndex + ".bmp"), System.Drawing.Imaging.ImageFormat.Bmp);
                        Application.DoEvents();
                    }

                    if (fg == null)
                    {
                        return;
                    }
                }
            }
        }

        public void ThreadProc()
        {
            try
            {
                MethodInvoker mi = new MethodInvoker(this.frameExtract);
                this.BeginInvoke(mi);
            }
            catch (ThreadInterruptedException e)
            {
                Console.WriteLine("Interruption",e);
            }
            catch (Exception we)
            {
                Console.WriteLine("Exceptiom",we);
            }
        }

        private void button43_Click(object sender, EventArgs e)//Extract button
        {
            fThread = new Thread(new ThreadStart(ThreadProc));
            fThread.IsBackground = true;
            fThread.Start();
        }

        private void button44_Click(object sender, EventArgs e)//Stop Button
        {
            fThread.Interrupt();
            fThread = null;
        }

I see a lot, but I don't know what the rest of the class is doing, so I'm not sure what the problem is, it doesn't seem to be syntax. But I recommend using a background worker class, its easier to use, and returns some handy information, plus has a simple cancelasync method.

But I don't know what the references are too, Im not sure how this program extention you are using works, but to get it to work in a 2nd thread, the user control, or dll, or whatever it is you are using should be instantiated inside the new thread, and the collection of data to be processed by it should be passed to it as an object in an argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.