diff --git a/WIndows.Forms/Contexts/PreventShutdownContext.cs b/WIndows.Forms/Contexts/PreventShutdownContext.cs
index d50c6d0d..a4249b19 100644
--- a/WIndows.Forms/Contexts/PreventShutdownContext.cs
+++ b/WIndows.Forms/Contexts/PreventShutdownContext.cs
@@ -27,14 +27,18 @@ namespace Vanara.Windows.Forms.Forms
///
public class PreventShutdownContext : IDisposable
{
+ private const int WM_QUERYENDSESSION = 0x11;
private HandleRef href;
- /// Initializes a new instance of the class.
- /// The or that contains a valid window handle.
+ /// Initializes a new instance of the class.
+ /// The that contains a valid window handle.
/// The reason the application must block system shutdown. Because users are typically in a hurry when shutting down the system, they may spend only a few seconds looking at the shutdown reasons that are displayed by the system. Therefore, it is important that your reason strings are short and clear.
- public PreventShutdownContext(Control window, string reason)
+ /// If set to , this class will return false to the WM_QUERYENDSESSION by handling the e.Cancel response to Form.FormClosing.
+ public PreventShutdownContext(Form window, string reason, bool tryToPreventShutdown = false)
{
href = new HandleRef(window, window.Handle);
+ if (tryToPreventShutdown)
+ window.FormClosing += (s, e) => e.Cancel = true;
Reason = reason;
}