I am experiencing the following error within a Windows Service I have created:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.StubHelpers.InterfaceMarshaler.ConvertToManaged(IntPtr pUnk, IntPtr itfMT, IntPtr classMT, Int32 flags)
Basically I have a windows service that creates 3 separate threads that use a WebBrowser control to take screen shots (.jpg) of websites. Here is the code:
private bool RunService = false;
protected override void OnStart(string args)
RunService = true;
Thread workerThread = new Thread;
for (int i = 0; i < workerThread .Length; i++)
workerThread [ i ] = new Thread(new ThreadStart(ArchiveWebsites));
workerThread [ i ].SetApartmentState(ApartmentState.STA);
workerThread [ i ].Start();
private void ArchiveWebsites()
WebBrowser browser = new WebBrowser();
browser.Size = new Size(800, 600);
browser.ScrollBarsEnabled = false;
browser.ScriptErrorsSuppressed = true;
browser.AllowNavigation = true;
string url = GetUrlToArchive(); //method not provided...can substitute http://www.google.com
while (browser.ReadyState != WebBrowserReadyState.Complete)
//take screen shot after document is rendered
The code that throws the exception is while (browser.ReadyState != WebBrowserReadyState.Complete) and I cannot reproduce the error consistently. For example, if I recursively take screen shots of 10000 websites I get approximately 2 errors (sometimes more, sometimes less). My short term fix is to immediately take the screen shot again if it fails. This seems to work because it has never failed on the second attempt.
I've checked out the hotfix http://support.microsoft.com/kb/923028 and the server has the latest version of the framework.
Anyone have any ideas