Shawn_VM

I am experiencing the following error within a Windows Service I have created:

Exception:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Stack Trace:

at System.StubHelpers.InterfaceMarshaler.ConvertToManaged(IntPtr pUnk, IntPtr itfMT, IntPtr classMT, Int32 flags)

at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()

at System.Windows.Forms.WebBrowser.get_Document()

at System.Windows.Forms.WebBrowser.get_ReadyState()

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[3];

for (int i = 0; i < workerThread .Length; i++)

{

workerThread [ i ] = new Thread(new ThreadStart(ArchiveWebsites));

workerThread [ i ].SetApartmentState(ApartmentState.STA);

workerThread [ i ].Start();

}

}

[STAThread]

private void ArchiveWebsites()

{

WebBrowser browser = new WebBrowser();

browser.Size = new Size(800, 600);

browser.ScrollBarsEnabled = false;

browser.ScriptErrorsSuppressed = true;

browser.AllowNavigation = true;

while (RunService)

{

string url = GetUrlToArchive(); //method not provided...can substitute http://www.google.com

if(!string.IsNullOrEmpty(url))
{

browser.Navigate(url);

while (browser.ReadyState != WebBrowserReadyState.Complete)

{

Application.DoEvents();

}

//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



Re: Visual C# General Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

OmegaMan

Does the same thing occur if you do your tests from a console application Maybe the constraints of being in a service is kernal to the issues that are occuring





Re: Visual C# General Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Figo Fei - MSFT

We are changing the issue type to ˇ°Commentˇ± because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to ˇ°Questionˇ± by editing your initial post and changing the radio button at the top of the post editor window.

If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

Thank you!