hey can anyone help me,...........
there is a little delay when i press NumPad0 or NumPad3 at the same time when video is on...ie. the count is displayed a somewhat a little delay in the label1 and label2

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading;
using System.Resources;
namespace MultipleKeys
{
    public partial class MainForm : Form
    {
        int lefts = 0;
        int rights = 0;
        public MainForm()
        {
            InitializeComponent();
        }
        private void MainFormKeyUp(object sender, KeyEventArgs e)
        {   
        }
        private void MainFormKeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.NumPad3)
            {
                if (rights >= 0 && lefts == 0 && rights < 30)
                {
                    timer1.Enabled = true;
                }
                if (lefts >0)
                {
                    timer4.Enabled = true;
                }
            }
            if (e.KeyCode == Keys.NumPad1)
            {
                if (rights > 0)
                {
                    timer2.Enabled = true;
                }
                if (rights <= 0 && lefts < 30)
                {
                    timer3.Enabled = true;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = (200); 
            rights++;
            label1.Text = "" + rights;
            label1.Refresh();
            timer1.Enabled = false;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Interval = (200);
            rights--;
            label1.Text = "" + rights;
            label1.Refresh();
            timer2.Enabled = false;
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer3.Interval = (200);
            lefts++;
            label2.Text = "" + lefts;
            label2.Refresh();
            timer3.Enabled = false;
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            timer4.Interval = (200);
            lefts--;
            label2.Text = "" + lefts;
            label2.Refresh();
            timer4.Enabled = false;
        }

        private void startvideo_Click(object sender, EventArgs e)
        {
            this.WebCamCapture.TimeToCapture_milliseconds = 10;
            this.WebCamCapture.Start(0);
        }

        private void stopvideo_Click(object sender, EventArgs e)
        {
            this.WebCamCapture.Stop();
            Capturing.Enabled = false;
        }

        private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
        {
            this.pictureBox5.Image = e.WebCamImage;
        }
    }
}

Is your video streaming running on the same thread as your UI? From your code it appears to be doing so. Video capture is slow and is causing your UI to pause while it gets and loads the image. Move the video capture to a different thread.

Did you read the pages I linked? At the bottom of the tutorial is a link that gives step by step source code.

I usually opt to use the Thread class, with mutexes,
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx
and with a function and delegates.

Delegates will probably be needed to access any controls on your form because the controls must be accessed on the same thread they were created.

You can define a custom delegate for a function you wish to call to interact with your controls, or use a pre-defined one : Action
http://msdn.microsoft.com/en-us/library/018hxwa8.aspx

One can use the form's Invoke() member function to call the functions being "pointed to" by the delegates.

I strongly suggest you read about some Multi-threading fundamentals before attempting this.

Of course that's just one way to do it, I never liked the backgroundworker, personally.

Backgroundworker is easy to use, and based on his postings I judged his skill level at "look for easy things to use".

Mutex is 10 times slower than just a basic lock, and probably overkill for this application. I'm not even sure he'd need to use either in this case, as Invoke does that work for you.

Mutex is usually used when you need to share resources between applications (not just threads), like if you are using memory mapped files of Isolated Storage. You can also use a Mutex to prevent a 2nd copy of your program from running.

But I think we are getting off topic here.

Edited 5 Years Ago by Momerath: n/a

Personally, when doing video-related things I use Monitor.Enter/Exit manually. One UI thread, one capture thread, one saving thread. I find it really helps with performance. So if you would prefer threads over BackgroundWorkers, just keep that kind of performance thing in mind.

Comments
Good info.

can we put all those key handling function which i've mention in the program within backgroundworker

Yes you can, but ask yourself "Do I want to put multiple key handling events as background workers or do I want to put the single video update in a background worker?"

STILL NOT WORKING...........

using System;
using System.Collections.Generic;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading;
using System.Resources;
namespace MultipleKeys
{
    public partial class MainForm : Form
    {
        int lefts = 0;
        int rights = 0;
        private BackgroundWorker Worker = new BackgroundWorker();
        public MainForm()
        {
            InitializeComponent();
          //m_AsyncWorker.WorkerReportsProgress = true;
            Worker.DoWork += new DoWorkEventHandler(DoWork);
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bwAsync = sender as BackgroundWorker;
            this.WebCamCapture.TimeToCapture_milliseconds = 10;
            this.WebCamCapture.Start(0);
        }


        private void WebCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e)
        {
            this.pictureBox5.Image = e.WebCamImage;
        }



        private void MainFormKeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.NumPad3)
            {
                if (rights >= 0 && lefts == 0 && rights < 30)
                {
                    timer1.Enabled = true;
                }
                if (lefts >0)
                {
                    timer4.Enabled = true;
                }
            }
            if (e.KeyCode == Keys.NumPad1)
            {
                if (rights > 0)
                {
                    timer2.Enabled = true;
                }
                if (rights <= 0 && lefts < 30)
                {
                    timer3.Enabled = true;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Interval = (200); 
            rights++;
            label1.Text = "" + rights;
            label1.Refresh();
            timer1.Enabled = false;
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Interval = (200);
            rights--;
            label1.Text = "" + rights;
            label1.Refresh();
            timer2.Enabled = false;
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer3.Interval = (200);
            lefts++;
            label2.Text = "" + lefts;
            label2.Refresh();
            timer3.Enabled = false;
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            timer4.Interval = (200);
            lefts--;
            label2.Text = "" + lefts;
            label2.Refresh();
            timer4.Enabled = false;
        }


        private void stopvideo_Click(object sender, EventArgs e)
        {
            this.WebCamCapture.Stop();
            Capturing.Enabled = false;
        }
        
    }
}
This article has been dead for over six months. Start a new discussion instead.