FormClosing中设置e.Cancel = true何时无法阻止系统注销?
e.Cancel = true in FormClosing Won't Block System Sign-Out Great question—let's unpack exactly when setting e.Cancel = true in a WinForms FormClosing event won't stop a system sign-out, based on how Windows handles app shutdown and WinForms behavior:
1. You're only canceling for CloseReason.UserClosing
When Windows initiates a sign-out or shutdown, it sends your app a FormClosing event with a different close reason: CloseReason.WindowsShutDown (or CloseReason.TaskManagerClosing if Task Manager is forcing the app closed).
If your code only sets e.Cancel = true when e.CloseReason == CloseReason.UserClosing (like handling the user clicking the window's X button), this logic won't kick in during a system sign-out. The app will just close normally, no blocking involved.
This is almost certainly the difference between your two apps: the one that minimizes to the tray probably doesn't cancel the close for WindowsShutDown, while the dialog app either cancels all close reasons or explicitly handles the shutdown case.
2. Windows hits its shutdown timeout
Windows gives apps a limited window (usually around 5 seconds by default) to respond to a shutdown request. If your app takes too long to process the FormClosing event—like showing a dialog the user doesn't interact with quickly, or running long code after setting e.Cancel = true—Windows will ignore the cancellation and force-close the app anyway. This isn't a failure of e.Cancel = true itself, but a system safeguard to prevent apps from hanging the shutdown process.
3. The event handler is attached to a secondary form
If your e.Cancel = true logic is only on a secondary form (not the app's main window), Windows will still proceed with shutdown. The system primarily checks the main window's response to the shutdown request; canceling a secondary form's close won't block the overall process.
4. The app is running in a restricted context
In rare cases, if your app is running with limited permissions (like a service, or in a sandboxed environment), it might not have the ability to block system shutdown. Windows may ignore the cancellation request if the app doesn't have the necessary privileges to interfere with system-level operations.
5. You're using Application.Exit() to trigger the close
If you initiate the app's close via Application.Exit(), the FormClosing event fires with CloseReason.ApplicationExitCall. If your code only cancels for other close reasons, this won't block the exit—and importantly, Application.Exit() can override e.Cancel = true in some cases, since it's an explicit request to shut down the entire application.
Quick Verification Tip
To confirm which scenario applies to your apps, add logging to the FormClosing event to record e.CloseReason every time it fires. When you trigger a sign-out, check what reason is logged. If it's WindowsShutDown and your code isn't canceling that case, that's why the app doesn't block sign-out.
内容的提问来源于stack exchange,提问作者ispiro

