我有一个非常简单的 Windows 应用程序,其中包含一些控件、DataGridView 和 Adobe PDF 查看器控件。该应用程序运行良好,除了在控件之间切换焦点时,我经常遇到以下异常。
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.AxHost.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
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)
at Scan_Manager.Program.Main() in D:\Checkouts\Code\Scan Manager\Scan Manager\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
有人指出我正确的方向吗?
谢谢,乔
更新
经过更多尝试后,它只会在 Adobe 控件失去焦点时发生。我可以通过禁用它的 Tabstop 来解决一些问题,但遗憾的是,当我使用 LoadFile 加载 PDF 时,它会自动获得焦点(然后当我关闭 Tab 时应用程序崩溃)
最佳答案
这可能不会有帮助,但我只在使用第三方 COM 组件(如 Adobe Acrobat Reader)或在做一些需要大量内存。
这是非常难以解决的错误之一,对此问题的答案往往因导致问题的原因而异。
这可能有些牵强,但如果您直接在应用程序中托管 Acrobat Reader,则可以尝试使用 WebBrowser 控件来显示文档并将 DocumentSource 设置为 PDF 的位置。 WebBrowser 控件是,我相信(虽然我不确定)基于 Internet Explorer,并且 Internet Explorer 可能比纯 .Net WinForms 应用程序更好地托管这些控件。我已经成功地以这种方式显示其他文件,而不是尝试包含第三方 COM 引用。如果确实有效,请告诉我。
关于c# - 在 .NET 应用程序中使用 Tab 键更改焦点的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1185195/