在我的Windows窗体应用程序中,如果单击其中一个项目,我必须打开一个带有特定文件夹的新资源管理器窗口。我正在侦听MouseUp事件(因为我已经有一些点击检测,我需要点击坐标),如果我打开一个新的资源管理器窗口
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
Process.Start(@"C:\");
}
它会两次打开资源管理器窗口。它绝对与Process.Start(@"C:\");
有关,因为当我将线路切换到正常的控制台输出时,它只执行一次。
有没有办法将事件标记为已处理或只是忽略第二次执行?
答案 0 :(得分:8)
这是一个 re-entrancy bug ,非常类似于在代码中使用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()干净地延迟棘手的代码,以便它在之后立即运行/ em>事件被处理。像这样:
private void listView1_MouseUp(object sender, MouseEventArgs e) {
this.BeginInvoke(new Action(() => {
Process.Start(@"C:\");
}));
}