1

简洁版本:

我有一个 WinForms 应用程序,它使用 .NET 4.0 WebBrowser 控件来显示包含 Flash SWF 文件的 HTML 页面。SWF 文件通过一些 Javascript/jQuery/swf)iobject 魔术设置。我定期遇到一个 SWF 文件,该文件将导致 AccessViolationException 停止我的程序。据我所知,这发生在显示一个页面和启动另一个页面之间的清理过程中(想想网站的自动幻灯片放映)。

我添加了 AppDomain.CurrentDomain.UnhandledException 和 Application.ThreadException 处理程序,我的程序仍然停止并在屏幕上显示一个对话框。我想阻止对话框出现 - 如何捕获这些类型的异常并抑制它们?我可以重新启动我的程序或其他东西 - 我无法显示对话框。

血腥细节:

我有上面提到的异常处理程序,在我通过 InvokeScript() 在我的 WebBrowser DOM 中调用行为的地方周围还有一些 try/catch 逻辑。但我所拥有的一切似乎都没有被调用。

生成的异常如下所示:

Faulting application name: MyProgram.exe, version: 1.0.0.0, time stamp: 0x50096c59
Faulting module name: Flash64_11_3_300_257.ocx, version: 11.3.300.257, time stamp: 0x4fc81d71
Exception code: 0xc0000005
Fault offset: 0x000000000022b1db
Faulting process id: 0x10d0
Faulting application start time: 0x01cd668d0db086f8
Faulting application path: C:\Users\AUser\AppData\Local\Apps\2.0\AWMVPDR4.HEJ\Z9EP5M32.MQ4\mmm...tion_2c82cc3ef3e7d3e9_0001.0000_6ffd6d2ca43477ab\MyProgram.exe
Faulting module path: C:\Windows\system32\Macromed\Flash\Flash64_11_3_300_257.ocx
Report Id: a9f6977f-d284-11e1-a447-00187d1f4237

然后这个:

Application: MyProgram.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at System.Windows.Forms.UnsafeNativeMethods+IDispatch.GetIDsOfNames(System.Guid ByRef, System.String[], Int32, Int32, Int32[])
   at System.Windows.Forms.HtmlDocument.InvokeScript(System.String, System.Object[])
   at MyProgram.InvokeScriptExtension+<>c__DisplayClass2.<InvokeScript>b__0()
   at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry)
   at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
   at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.WebBrowserBase.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.WebBrowser.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
   at MyProgram.Program.Main()

我的挑战是阻止该异常在屏幕上显示任何内容。

4

2 回答 2

1

您可以包含禁止异常对话的 windows api 中的 pinvoke 方法。

这是图书馆。 http://www.pinvoke.net/default.aspx/kernel32.seterrormode

这是 C++ 文档。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms680621(v=vs.85).aspx

你把它调用到你的代码中

class MainClass
{
[DllImport("kernel32.dll")]
static extern ErrorModes SetErrorMode(ErrorModes uMode);

[Flags]
public enum ErrorModes : uint
{
    SYSTEM_DEFAULT = 0x0,
    SEM_FAILCRITICALERRORS = 0x0001,
    SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
    SEM_NOGPFAULTERRORBOX = 0x0002,
    SEM_NOOPENFILEERRORBOX = 0x8000
}
[System.STAThreadAttribute()]
static void Main()
{
    SetErrorMode(ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);
  // this function prevents error dialog box to show up after application crash
}
于 2013-08-02T20:56:17.820 回答
0

好的 - 发现了一些关于 .NET 4 的信息。这不是新的,对我来说只是新的。对非托管内容的异常处理与以前的 .NET 版本(我从未真正使用过,对 .NET 编程有点新手)不同。以下链接有望提供一些指导和启发......

http://msdn.microsoft.com/en-us/library/dd638517.aspx

是否可以在 .NET 中捕获访问冲突异常?

http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

有人声称您可以向 app.config 添加参数并获取旧的异常处理行为。这对我不起作用,但我不会声称我做得正确。将装饰器添加到 Main() 例程以启用 try/catch 行为的描述确实解决了我的问题。

于 2012-08-07T15:12:45.267 回答