c# - 在 .NET 应用程序中使用 Tab 键更改焦点的异常

标签 c# .net exception focus

我有一个非常简单的 Windows 应用程序,其中包含一些控件、DataGridView 和 Adob​​e 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: 

有人指出我正确的方向吗?

谢谢,乔

更新

经过更多尝试后,它只会在 Adob​​e 控件失去焦点时发生。我可以通过禁用它的 Tabstop 来解决一些问题,但遗憾的是,当我使用 LoadFile 加载 PDF 时,它会自动获得焦点(然后当我关闭 Tab 时应用程序崩溃)

最佳答案

这可能不会有帮助,但我只在使用第三方 COM 组件(如 Adob​​e 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/

相关文章:

c# - 将数据作为原始数据传递给打印机

c# - Async Await 和 ContinueWith 未按预期工作

c# - 动态代码片段 c# visual studio

C# .Net Core 依赖注入(inject),向构造函数注入(inject)多个参数

c# - 如何在 C# Windows 中获取文件大小、文件名、文件扩展名?

c# - 架构师能说对吗 "MVVM only splits the code behind to multiple (3) files "

.net - .NET 设计指南是否建议在 IEnumerable<T> 上返回 List<T>?

java - 异常处理无限循环

c# - C# 赋值变量时的异常处理

java - 什么时候抛出 "throws Exception"?