2012-08-07 42 views
3

可能重复:
Troubleshooting .NET “Fatal Execution Engine Error”致命的执行错误的Browser.ReadyState

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

运行时遇到致命错误。错误 的地址为0xed40646c,位于线程0x2044。错误代码是0xc0000005。 此错误可能是CLR中的错误,也可能是用户代码中不安全或不可验证的 部分中的错误。此错误的常见来源包括用户 编组错误COM-interop或PInvoke,这可能会损坏 堆栈。

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

这是造成问题的代码是这一个:

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次运行。这似乎是非常随机的。

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

+0

它可能不相关,但紧密的循环检查RedayState环立即警钟。您是否可以不在WebBrowser上监听DocumentCompleted事件? – 2012-08-07 11:48:16

+1

我曾经这样做(听取事件),但它使其工作异步,我需要通过许多网页,以等待秒为它不是一个选项。所以不,我不能那样做。这段代码很好地运行,直到你大量使用它。 – Aabela 2012-08-07 12:37:49

回答

1

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

什么u必须做的是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) 
      { 
      } 
     } 
相关问题