1

I have a method which takes in the webcontrol and other parameters in order to take a screen shot of the webcontrol every 200 ms then i have another method which finds a small picture in the screenshot and if the method finds it, a true is returned.

The goal:

take a screenshot while the webcontrol loads during a specific timespan.

The issue:

the webcontrol and the form UI freeze.

I tried a different thread but i get an exception as the webcontrol was created in a different thread.

The code:

 private  bool Exists()
        {

            bool exists=   findImage.imageExists(conn.imgLoaded, 30,FUTWebControl).Result.Item1;




            return exists;
        }


        public async Task< Tuple<bool, Point>>imageExists(String IconPath, Double timeSpan, WebControl webControl)
        {
            int waitTime = Convert.ToInt32(timeSpan * 1000);
            Tuple<bool, Point> imageFound = new Tuple<bool, Point>(false, new Point(0, 0));
            Stopwatch timer = new Stopwatch();
            timer.Start();
            while (true)
            {
                await Task.Delay(200);

                imageFound = imageExists(IconPath,webControl);
                if (imageFound.Item1 || timer.ElapsedMilliseconds >= waitTime)
                    break;
            }

            return imageFound;
        }

       public Tuple<bool, Point> imageExists(String IconPath, WebControl webControl)
        {
            Tuple<Point, Size> locSize = getIconLocation(IconPath,webControl);
            if (locSize.Item1 != new Point(0, 0))
                return new Tuple<bool, Point>(true, getClickLocation(locSize));
            return new Tuple<bool, Point>(false, getClickLocation(locSize));
        }

     private Tuple<Point, Size> getIconLocation(String IconPath, WebControl webControl)
        {
            Point location = new Point();
            Size imgSize = new Size(0, 0);       

            using (Bitmap bmpScreenCapture = new Bitmap(webControl.Width, webControl.Height))
            {
                webControl.DrawToBitmap(bmpScreenCapture, new Rectangle(0, 0, webControl.Width, webControl.Height));

                using (Bitmap sub = new Bitmap(IconPath))
                {

                     location = getsubLocation(bmpScreenCapture, sub);
                    bmpScreenCapture.Save(@"C:\screenshot.bmp", ImageFormat.Bmp);

                    imgSize = sub.Size;
                }

            }


            return new Tuple<Point, Size>(location, imgSize);
        }
user3726459
  • 172
  • 1
  • 14
  • not at all, two separate problems. The web control can not be accessed from another different thread. I found a solution by calling Application.DoEvents() in a while loop and using timer events which works , application seems buggy while it is running but it works. – user3726459 Dec 13 '15 at 08:52
  • You need to call invoke when accessing items on the ui from a different thread. Though async doesn't actually call on a different thread without calling Task.Run (or similar) so you're running on and blocking the ui thread. Even though you call that second method with an await, the first isn't marked async and calling Result will block until complete. The link above is definitely relevant. Also don't call DoEvents – pinkfloydx33 Dec 13 '15 at 10:55
  • first, I can not call the webbrowser component from another thread than the original which it was created on. it is a limitation of the component and it will throw an exception. Secondly, the Application.Doevents solved the freezing issue caused by the while loop freezing the UI. when I remove it from the loop. Calling Application.DoEvents completely solved my issue. thx for the input – user3726459 Dec 13 '15 at 20:04
  • 1. Actually it's not a limitation of your Web browser control, it's a limitation of Every control accessed from a thread other than the one it was created on (the UI thread) 2. DoEvents may 'fix' your issue but it is dangerous. It causes messages in the message queue to be processed but will cause problems. It's not the same as DoEvents from Vb6. I recommended googling about why it's bad – pinkfloydx33 Dec 13 '15 at 20:15

0 Answers0