这是一个重入错误,与您在代码中使用 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:\");
}));
}