ShutdownBlockReason适用于Windows 7,但不适用于8

考虑下面的代码:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool blocked = false; private string times = ""; protected override void WndProc(ref Message aMessage) { const int WM_QUERYENDSESSION = 0x0011; const int WM_ENDSESSION = 0x0016; if (blocked && (aMessage.Msg == WM_QUERYENDSESSION || aMessage.Msg == WM_ENDSESSION)) return; base.WndProc(ref aMessage); } [DllImport("user32.dll")] public extern static bool ShutdownBlockReasonCreate(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string pwszReason); [DllImport("user32.dll")] public extern static bool ShutdownBlockReasonDestroy(IntPtr hWnd); string path = "times.txt"; private void button1_Click(object sender, EventArgs e) { if (ShutdownBlockReasonCreate(this.Handle, textBox1.Text)) { blocked = true; times += "Creating: " + DateTime.Now + "\r\n"; File.WriteAllText(path, times); } Thread.Sleep(10000); times += "About to end: " + DateTime.Now + "\r\n"; File.WriteAllText(path, times); if (ShutdownBlockReasonDestroy(this.Handle)) { times += "Ended: " + DateTime.Now + "\r\n"; File.WriteAllText(path, times); blocked = false; } } } 

按下button1,并在10秒内closures系统。

Windows 7计算机上,它按照预期的方式执行 – 再次通电后,会显示times.txt文件,并显示正确的时间。

在Windows 8上(embedded式,并在Windows 10上testing,只是为了更好的衡量,与8相同的结果),这似乎忽略了ShutdownBlockReason函数,因为times.txt文件只有Creating:行。

这些系统之间有什么不同的处理方法我不知道?