【问题标题】:Subclassing a window from a thread in c#从c#中的线程子类化窗口
【发布时间】:2010-03-23 23:33:27
【问题描述】:

我正在创建一个寻找窗口的线程。当它找到窗口时,它会覆盖它的 windowproc,并处理 WM_COMMAND 和 WM_CLOSE。

以下是查找窗口并将其子类化的代码:

public void DetectFileDialogProc()
{
   Window fileDialog = null;
   // try to find the dialog twice, with a delay of 500 ms each time
   for (int attempts = 0; fileDialog == null && attempts < 2; attempts++)
   {
      // FindDialogs enumerates all windows of class #32770 via an EnumWindowProc
      foreach (Window wnd in FindDialogs(500))
      {            
         IntPtr parent = NativeMethods.User32.GetParent(wnd.Handle);
         if (parent != IntPtr.Zero)
         {
            // we're looking for a dialog whose parent is a dialog as well
            Window parentWindow = new Window(parent);
            if (parentWindow.ClassName == NativeMethods.SystemWindowClasses.Dialog)
            {
               fileDialog = wnd;
               break;
            }
         }
      }
   }
   // if we found the dialog
   if (fileDialog != null)
   {
      OldWinProc = NativeMethods.User32.GetWindowLong(fileDialog.Handle, NativeMethods.GWL_WNDPROC);
      NativeMethods.User32.SetWindowLong(fileDialog.Handle, NativeMethods.GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(new WindowProc(WndProc)).ToInt32());
   }
}

还有windowproc:

public IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
   lock (this)
   {
      if (!handled)
      {
         if (msg == NativeMethods.WM_COMMAND || msg == NativeMethods.WM_CLOSE)
         {       
            // adding to a list. i never access the window via the hwnd from this list, i just treat it as a number
            _addDescriptor(hWnd);
            handled = true;
         }
      }
   }
   return NativeMethods.User32.CallWindowProc(OldWinProc, hWnd, msg, wParam, lParam);
}        

这一切在正常情况下运行良好。但我看到了两个不良行为实例,按不良顺序排列:

  1. 如果我在一分钟左右没有关闭对话框,应用程序就会崩溃。这是因为线程正在收集垃圾吗?就 GC 可以告诉线程已完成而言,这有点道理?如果是这种情况,(我不知道是这样),只要对话框存在,我怎样才能让线程一直存在?

  2. 如果我立即使用“X”按钮 (WM_CLOSE) 关闭对话框,应用程序将崩溃。我相信它在 windowproc 中崩溃了,但我无法在其中找到断点。我收到 AccessViolationException,异常显示“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”这是一个比赛条件,但我不知道。仅供参考,我在处理完命令后一直在重置旧的 windowproc,但崩溃的频率更高!

关于如何解决这些问题有什么想法吗?

【问题讨论】:

    标签: c# multithreading race-condition subclassing


    【解决方案1】:

    我可以提出两点观察......

    • 在您的DetectFileDialogProc 中,您将wnd 与null 进行比较,即IntPtr 类型是吗?如果是这样,比较检查应该是if (wnd &gt; IntPtr.Zero){ .... }
    • 在您的WndProc 中,您正在使用this 变量作为lock,这是一件坏事……您应该这样做private readonly object objLock = new object(); 并在您的WndProc 中使用这个@ 987654330@

    看看能不能解决问题....

    【讨论】:

      【解决方案2】:

      终于想出了一个解决方案,从不同的角度解决问题。我能够使用 SetWinEventHook 和选项 WINEVENT_OUTOFCONTEXT 在托管代码中设置系统范围的挂钩,该选项令人惊讶地具有以下属性:回调函数未映射到生成事件的进程的地址空间。 我捕获事件 EVENT_SYSTEM_DIALOGSTART 以在创建对话框时接收通知,并在其销毁时接收通知 EVENT_SYSTEM_DIALOGEND。

      【讨论】:

      • 它被称为“可访问性挂钩”,这些与常规的 Windows 挂钩不同
      猜你喜欢
      • 1970-01-01
      • 2017-06-21
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多