0

I have 2 backgroundworkers and 2 button that activate them. Also i have a function to FindBitmap :

private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
        {
            for (int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
            {
                for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
                {
                    for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
                    {
                        for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
                        {
                            Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
                            Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);

                            if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
                            {
                                goto notFound;
                            }
                        }
                    }
                    location = new Point(outerX - 8, outerY - 32);
                    return true;
                    notFound:
                    continue;
                }
            }
            location = Point.Empty;
            return false;
        }

When I added a second backgroundworker and I used inside function to Findbitmap i have an error : An exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll but was not handled in user code

Additional information: The object is currently in use elsewhere.

I know I can't use System.Drawing in 2 backgroundworkers in the same time. What else i can do to avoid this problem?

To take a Sscreenshoot i use :

   private Bitmap Screenshot()
        {
            RECT rt = new RECT();
            GetWindowRect(hwnd1, out rt);
            int width = rt.Right - rt.Left;
            int height = rt.Bottom - rt.Top;

            Bitmap bmp = new Bitmap(width, height);
            Graphics screenG = Graphics.FromImage(bmp);


            screenG.CopyFromScreen(rt.Left, rt.Top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

            bmp.Save(@"D:\printscreennnnnnnnnnn.jpg", ImageFormat.Jpeg);
            return bmp;
            //screenG.ReleaseHdc();





        }

I try to use RealeseHdc but it don't help. Also i try to just create a 2nd function : `Findbitmap1 () and take it into 2nd background worker but its the same it don't help I gonna add Screen of my:Form1.resx . and Screen exacly where is problem look : problem.jpg

I searched for solution but its kinda hard. I have and idea to create a new Form2.cs and take new background worker and copy everything and maybe this gonna work?

Adamszsz
  • 561
  • 3
  • 13
  • Possible duplicate of [InvalidOperationException - object is currently in use elsewhere](http://stackoverflow.com/questions/1851292/invalidoperationexception-object-is-currently-in-use-elsewhere) – Sinatr Mar 16 '17 at 09:14
  • Is it possible to create a new Form2.cs and copy there my backgroundworker with everyhitng findbitmap etc, and it's gonna work without any bug? how do you think – Adamszsz Mar 16 '17 at 09:35
  • Have you read answers from linked question? One possible solution is to clone bitmap before starting new thread. This way nor UI, not any of threads will concurrently access same bitmap. – Sinatr Mar 16 '17 at 09:39

0 Answers0