c# - Browser.ReadyState 上的致命执行错误

标签 c# winforms browser clr fatal-error

<分区>

Possible Duplicate:
Troubleshooting .NET “Fatal Execution Engine Error”

我的代码抛出致命执行错误。确切的错误是这样的:

The runtime has encountered a fatal error. The address of the error was at 0xed40646c, on thread 0x2044. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

据我所知,我没有使用不安全的用户代码。

导致问题的代码是这个:

WebClient client = new WebClient();

string pageHtml = client.DownloadString(url);

browser.ScriptErrorsSuppressed = true;

browser.DocumentText = pageHtml;

do
 {
  Application.DoEvents();

  } while (browser.ReadyState != WebBrowserReadyState.Complete); //CRASH OCCURS HERE

现在是关键。这段代码是循环运行的。每隔一段时间,它就会出现此错误。有时是在第 1000 次运行时。上次是第 5545 次运行。这似乎确实是非常随机的。

我该如何解决这个问题?或者我怎样才能获得更多信息来解决它?

最佳答案

我的解决方案基于 How to wait until WebBrowser is completely loaded in VB.NET?

您需要做的是将bool _pageReady 变量添加到Completed 事件。

void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var urlCurrent = e.Url.ToString();
            var browser = (WebBrowser)sender;

            if (!(urlCurrent.StartsWith("http://") || urlCurrent.StartsWith("https://")))
            {
                // in AJAX     
            }
            if (e.Url.AbsolutePath != browser.Url.AbsolutePath)
            {
                // IFRAME           
            }
            else
            {
                //  DOCUMENT IS LOADED 100%
                Debug.WriteLine("DocumentCompleted " + DateTime.Now.TimeOfDay.ToString());

                _pageReady = true; // Here it goes!!!! :)

                try
                {
                    mshtml.IHTMLDocument2 docs2 = (mshtml.IHTMLDocument2)web.Document.DomDocument;
                    mshtml.IHTMLDocument3 docs3 = (mshtml.IHTMLDocument3)web.Document.DomDocument;
                    mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)docs2.body;
                    mshtml.IHTMLElement2 root2 = (mshtml.IHTMLElement2)docs3.documentElement;

                    // Determine dimensions for the image; we could add minWidth here
                    // to ensure that we get closer to the minimal width (the width
                    // computed might be a few pixels less than what we want).
                    int width = Math.Max(body2.scrollWidth, root2.scrollWidth);
                    int height = Math.Max(root2.scrollHeight, body2.scrollHeight);

                    //get the size of the document's body
                    Rectangle docRectangle = new Rectangle(0, 0, width, height);

                    web.Width = docRectangle.Width;
                    web.Height = docRectangle.Height;

                    //if the imgsize is null, the size of the image will 
                    //be the same as the size of webbrowser object
                    //otherwise  set the image size to imgsize
                    Rectangle imgRectangle;
                    if (imgsize == null) imgRectangle = docRectangle;
                    else imgRectangle = new System.Drawing.Rectangle() { Location = new System.Drawing.Point(0, 0), Size = imgsize.Value };

                    //create a bitmap object 
                    __Bitmap = new Bitmap(imgRectangle.Width, imgRectangle.Height);

                    //Rectangle resolution = Screen.PrimaryScreen.Bounds;
                    //__Bitmap.SetResolution(resolution.Width, resolution.Height); 

                    //get the viewobject of the WebBrowser
                    IViewObject ivo = web.Document.DomDocument as IViewObject;

                    using (Graphics g = Graphics.FromImage(__Bitmap))
                    {
                        //get the handle to the device context and draw
                        IntPtr hdc = g.GetHdc();
                        ivo.Draw(1, -1, IntPtr.Zero, IntPtr.Zero,
                                 IntPtr.Zero, hdc, ref imgRectangle,
                                 ref docRectangle, IntPtr.Zero, 0);
                        g.ReleaseHdc(hdc);
                    }

                    //var randomPart = System.IO.Path.GetRandomFileName();
                    //__Bitmap.Save(@"D:\t" + randomPart + ".png");

                    if (CropRectangle != null)
                    {
                        if (CropRectangle.Width > 0 && CropRectangle.Height > 0)
                        {
                            Bitmap bmpCrop = __Bitmap.Clone(CropRectangle, __Bitmap.PixelFormat);
                            __Bitmap = bmpCrop;
                        }
                    }

                    //__Bitmap.Save(@"D:\cropped" + randomPart + ".png");

                    bitmapPointer = __Bitmap.GetHbitmap();
                }
                catch
                {
                    //System.Diagnostics.Process.GetCurrentProcess().Kill();
                }
            }
        }

也可以做类似的事情

public void HtmlCapture2()
        {
            try
            {
                if (web == null)
                    web = InitWebBrowser();

                web.Navigate(_navigateURL);

                try
                {
                    while (_pageReady == false) // YEAH!!!!!! IT IS WORKING!!!!
                    {
                        System.Windows.Forms.Application.DoEvents();
                     }
                    //Thread.Sleep(WaitForWebsite); --- It works but....
                    //while (web.ReadyState != WebBrowserReadyState.Complete) --- it gives an ERROR
                    //    System.Windows.Forms.Application.DoEvents();
                }
                catch (Exception)
                {
                }
            }
            catch (Exception)
            {
            }
        }

关于c# - Browser.ReadyState 上的致命执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845123/

相关文章:

php - 让浏览器缓存我的动态 PHP 样式表

c# - 在字符串中查找值

c# - 如何使用 OpenXML 从 Excel 工作表中检索选项卡名称

c# - 删除不是 ASCII 32 到 175 C# 的字符的更好方法

C# 按钮文本和调整大小

javascript - 如何使用 JavaScript 在浏览器中存储文件

c# - 仅对 KeyPress 事件中的文本输入起作用

C# 如何从 linq2sql 查询返回到 main

c# - datagridview向上滚动

asp.net - 如何判断用户的浏览器是否可以查看PDF文件