title: Visual C# BackgroundWorker Object is currently in use elsewhere
body: <p>Hello and thanks for reading my thread! I would like to seek advice for my code because after a lot of searching I couldn't find anything to solve this particular problem. I've googled and searched on stackoverflow and all the solutions somehow didn't work (or I didn't know how to implement them). Here is my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using AForge.Video;
  using AForge.Video.DirectShow;
  using System.Threading ;
  
      namespace Motion_Detection
      {
           public partial class Form1 : Form
          {
              private FilterInfoCollection VideoCaptureDevices;
              private VideoCaptureDevice FinalVideo;
  
          private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
          {
              Bitmap video = (Bitmap)eventArgs.Frame.Clone();
              pictureBox1.Image = video;
          }
  
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
              foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
              {
                  devicesList.Items.Add(VideoCaptureDevice.Name);
                  devicesList.SelectedIndex = 0;
              }
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[devicesList.SelectedIndex].MonikerString);
              FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
  
              FinalVideo.Start();
          }
  
          private void button2_Click(object sender, EventArgs e)
          {
              pictureBox1.Image = null;
              FinalVideo.Stop();
          }
  
          private void button3_Click(object sender, EventArgs e)
          {
              worker.RunWorkerAsync();
          }
  
          private void button4_Click(object sender, EventArgs e)
          {
              worker.CancelAsync();
          }
  
          private void worker_DoWork(object sender, DoWorkEventArgs e)
          {
              while (!worker.CancellationPending)
              {
                  Bitmap map = new Bitmap(pictureBox1.Image); // this line throws the error
  
                  for (int x = 0; x &lt; pictureBox1.Width; x++)
                  {
                      for (int y = 0; y &lt; pictureBox1.Height; y++)
                      {
                          Color pixel = map.GetPixel(x, y);
  
                          if (pixel.R == 255 &amp;&amp; pixel.G == 0 &amp;&amp; pixel.B == 0)
                          {
                              // detected red pixel
                          }
                      }
                  }
  
                  Thread.Sleep(100); // check every 100ms or any other given interval
              }
          }
      }
  }
  </code></pre>
  
  <p>So I'm using the Aforge video dlls to access my web camera. This part works, I can access and read the stream from it, when I dump it into picturebox1 it shows up perfectly, without any lag whatsoever.</p>
  
  <p>Now I'm toying a bit around with motion detection and for the first, I wanted to see if I can detect a pixel of a certain color appearing in front of the camera. Because I need to loop through every pixel, I had to put this on a different thread else it kept freezing my GUI and the display started to lag.</p>
  
  <p>The issue is because I did this, I don't know how to properly access the picturebox.image content from the background worker without triggering the error from the title. Some people on the internet suggested using lock() but I never did this nor I know what I should lock() here. I never worked with multithreading before just because in the end I could never handle the access violations..</p>
  
  <p>To fix this problem I tried things like try finally block although even within the try block, I've gotten the same exception. I assume that there is a cleaner way to do what I've mentioned but I can't really get my head around which one that might be.</p>
  
  <p>I hope that my first post here on the forums was clear and understandable as possible.</p>
  
  <p>Thanks in Regards
  ~ Ilhan</p>