multithreading - Visual C# BackgroundWorker Object is currently in use elsewhere -


hello , reading thread! seek advice code because after lot of searching couldn't find solve particular problem. i've googled , searched on stackoverflow , solutions somehow didn't work (or didn't know how implement them). here 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); // line throws error                  (int x = 0; x < picturebox1.width; x++)                 {                     (int y = 0; y < picturebox1.height; y++)                     {                         color pixel = map.getpixel(x, y);                          if (pixel.r == 255 && pixel.g == 0 && pixel.b == 0)                         {                             // detected red pixel                         }                     }                 }                  thread.sleep(100); // check every 100ms or other given interval             }         }     } } 

so i'm using aforge video dlls access web camera. part works, can access , read stream it, when dump picturebox1 shows perfectly, without lag whatsoever.

now i'm toying bit around motion detection , first, wanted see if can detect pixel of color appearing in front of camera. because need loop through every pixel, had put on different thread else kept freezing gui , display started lag.

the issue because did this, don't know how access picturebox.image content background worker without triggering error title. people on internet suggested using lock() never did nor know should lock() here. never worked multithreading before because in end never handle access violations..

to fix problem tried things try block although within try block, i've gotten same exception. assume there cleaner way i've mentioned can't head around 1 might be.

i hope first post here on forums clear , understandable possible.

thanks in regards ~ ilhan

you cant/shouldn't access picturebox1 unless on ui thread.

i think need this:

private void getimage(out bitmap img) {     img = new bitmap(picturebox1.image); }  void worker_dowork(object sender, doworkeventargs e) {     bitmap img = null;     invoke(new action(() => getimage(out img)));     // want bitmap } 

accessing control on winform throw exception if not on ui thread. can find out if on correct thread picturebox1.invokerequired. calling invoke send message ui thread passed delegate , wait passed delegate complete. calling begininvoke send message not wait.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -