如何防止webcontrol在while循环中挂起?

时间:2015-12-13 07:03:05

标签: c# winforms

我有一个方法,它接受webcontrol和其他参数,以便每200毫秒拍摄一次webcontrol的屏幕截图然后我有另一种方法在屏幕截图中找到一个小图片,如果方法找到它,则为真归还。

目标:

在webcontrol在特定时间段内加载时截取屏幕截图。

问题:

webcontrol和表单UI冻结。

我尝试了一个不同的线程,但我得到一个例外,因为webcontrol是在另一个线程中创建的。

代码:

 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);
        }

0 个答案:

没有答案