c# - 使用 Process.Start() 时 Windows 窗体 MouseUp 触发两次

标签 c# wpf winforms mouseup

在我的 Windows 窗体应用程序中,如果单击其中一个项目,我必须打开一个包含特定文件夹的新资源管理器窗口。我正在监听 MouseUp 事件(因为我已经有一些需要点击坐标的命中检测)现在如果我通过

打开一个新的资源管理器窗口
private void listView1_MouseUp(object sender, MouseEventArgs e)
    {
        Process.Start(@"C:\");
    }

它会打开资源管理器窗口两次。它肯定与 Process.Start(@"C:\"); 有关,因为当我将行切换到普通控制台输出时,它只执行一次。

有没有办法将事件标记为已处理或忽略第二次执行?

最佳答案

这是一个重入错误,与您在代码中使用 DoEvents() 时遇到的错误类型非常相似。但在这种情况下,错误是内置在操作系统中的。解决这个问题的工具是资源管理器,它在这段代码中有一个非常不寻常的激活模式。当您查看 Process.Start() 的返回值时,您可以看到一些东西,它是 null。当 Process.Start() 实际上没有启动进程时发生。

您可以通过使用仅命中第二次的条件断点来查看正在运行的错误。启用非托管调试和符号服务器后,调用堆栈如下所示:

WindowsFormsApp1.exe!WindowsFormsApp1.Form1.listView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) Line 19 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.OnMouseUp(System.Windows.Forms.MouseEventArgs e) Line 9140    C#
System.Windows.Forms.dll!System.Windows.Forms.ListView.WndProc(ref System.Windows.Forms.Message m) Line 6298    C#
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) Line 14236  C#
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) Line 14291    C#
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd, int msg, System.IntPtr wparam, System.IntPtr lparam) Line 780 C#
[Native to Managed Transition]  
user32.dll!__InternalCallWinProc@20()  Unknown
user32.dll!UserCallWinProcCheckWow()    Unknown
user32.dll!CallWindowProcW()    Unknown
comctl32.dll!_CallNextSubclassProc@20()    Unknown
comctl32.dll!_CallNextSubclassProc@20()    Unknown
comctl32.dll!_MasterSubclassProc@16()  Unknown
user32.dll!__InternalCallWinProc@20()  Unknown
user32.dll!UserCallWinProcCheckWow()    Unknown
user32.dll!DispatchMessageWorker()  Unknown
user32.dll!_DispatchMessageW@4()   Unknown
shell32.dll!SHProcessMessagesUntilEventsEx(struct HWND__ *,void * *,unsigned long,unsigned long,unsigned long,unsigned long)    Unknown
shell32.dll!CShellExecute::_RunThreadMaybeWait(bool)    Unknown
shell32.dll!CShellExecute::ExecuteNormal(struct _SHELLEXECUTEINFOW *)   Unknown
shell32.dll!ShellExecuteNormal(struct _SHELLEXECUTEINFOW *) Unknown
shell32.dll!_ShellExecuteExW@4()   Unknown
System.ni.dll!71db9903()    Unknown
[Frames below may be incorrect and/or missing, native debugger attempting to walk managed call stack]   
[Managed to Native Transition]  
System.dll!System.Diagnostics.ShellExecuteHelper.ShellExecuteFunction() Unknown
System.dll!System.Diagnostics.ShellExecuteHelper.ShellExecuteOnSTAThread()  Unknown
System.dll!System.Diagnostics.Process.StartWithShellExecuteEx(System.Diagnostics.ProcessStartInfo startInfo)    Unknown
System.dll!System.Diagnostics.Process.Start()   Unknown
System.dll!System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo startInfo)  Unknown
System.dll!System.Diagnostics.Process.Start(string fileName)    Unknown
WindowsFormsApp1.exe!WindowsFormsApp1.Form1.listView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) Line 19 C#

作恶者是 SHProcessMessagesUntilEventsEx() 函数,拼写“DoEvents”和实现“可能等待”的漫长方法,它导致 Winforms 应用程序的调度程序循环被重新输入。再次检测 MouseUp 条件并重新触发事件处理程序。

您无法修复操作系统,但解决方法是导致重入问题的事件处理程序的通用解决方法,您可以使用 BeginInvoke() 干净地延迟棘手的代码,以便它在 事件被处理。像这样:

private void listView1_MouseUp(object sender, MouseEventArgs e) {
    this.BeginInvoke(new Action(() => {
        Process.Start(@"C:\");
    }));
}

关于c# - 使用 Process.Start() 时 Windows 窗体 MouseUp 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43210194/

相关文章:

c# - 我什么时候应该使用 WaitHandle 而不是锁

c# - MvvM模型启动对话框

c# - 拖放时如何删除虚线矩形

c# - 如何在 C# 中拖动和移动形状

c# - MVC 获取所有 Action 方法

c# - 遍历单词列表并搜索文件夹以查找包含该单词的文件

c# - ASP.NET MVC 中的动态路由操作名称

c# - 如何将事件触发器添加到业务对象的数据模板?

c# - 如何获取用户从 winForm c# 中的 datagridview 输入的数据?

c# - Control.ParentChanged 事件何时发生?