2012-08-07 79 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) 
      { 
      } 
     }