Possible Duplicate:
Troubleshooting .NET “Fatal Execution Engine Error”
My code is throwing a Fatal Execution Error. The exact error is this:
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.
I'm not using unsafe user code as far as I know.
The code which is causing the issue is this one:
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
Now here's the kicker. This code is beign run on a loop. Every so often, it gives this error. Sometimes its on the 1000th run. Last time it was on the 5545th run. It seems to be very random indeed.
How do I solve this problem? Or how can I get more information to solve it?
My solution is based on How to wait until WebBrowser is completely loaded in VB.NET?
What u have to do is to add bool _pageReady
variable to Completed
event.
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();
}
}
}
and also to do something like
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)
{
}
}