C# WebBrowser Control System.AccessViolationExcept

2020-01-31 00:14发布

I have a program that uses the built in webbrowser control. At some point during the usage of this, I'm not sure at what point, but it appears to be random, I get the following error:

System.AccessViolationException

FullText = System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)

Does anyone have any clues as to why I would get this and how to prevent it?

8条回答
仙女界的扛把子
2楼-- · 2020-01-31 00:54

We're hitting this too. Inconsistently, we'll get this exception.

Some questions to help narrow this down: are you using any mshtml interfaces directly (e.g. mshtml.dll)? Doing any COM interop directly?

We've found that calling some of the COM MSHTML interfaces incorrectly can cause this.

We've also found that doing COM marshalling incorrectly can cause this.

If there's a bug in the MSHTML interface import that the built-in WebBrowser uses, it can cause this.

Accessing document IFRAME Elements from another domain can cause this.

It's possible that making WebBrowser calls when the document isn't quite ready may also cause this.

查看更多
聊天终结者
3楼-- · 2020-01-31 00:55

Just a suggestion, I'm no expert on this but have used the WebBrowser a lot in previous applications, but why dont you write a function to wait 1 second before attempting to pass the browser anything and always check the readystate beforehand aswell. Might slow it down a bit but it should make it bullet proof. :)

查看更多
登录 后发表回答