"shutdown.exe" 破坏应用程序设置
"shutdown.exe" destroys application settings
我有一个-window WPF应用程序(Win8.1/.net4.7),Window.Closing-事件未处理,Window.Closed-事件处理为如下:
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WinMainLocationX = this.Left; // ok
Properties.Settings.Default.WinMainLocationY = this.Top; // ok
Properties.Settings.Default.WinMain_size = new Size(this.Width, this.Height); // crucial setting
Properties.Settings.Default.WinMain_state = this.WindowState; // ok
Properties.Settings.Default.Save();
}
我每天通过一个包含 C:\WINDOWS\system32\shutdown.exe /s /t 20
的批处理文件关闭应用程序(此时始终处于空闲状态),此后什么也没有。这样电脑就可以正常关机了。 shutdown.exe
的参数可以通过命令行输入shutdown /?
.
查看
问题:每隔 7 或 8 天,window 大小就会以应用程序(早上启动后)看起来像这样的方式损坏:
如何保护我的应用程序设置免受 shutdown.exe
的干扰?
我认为问题出在应用程序 window 最小化时存储设置。在这种情况下,window 的宽度和高度将为 0。
您可以使用 window 的 RestoreBounds
属性 来获得独立于其当前状态的恢复大小:
Properties.Settings.Default.WinMainLocationX = this.RestoreBounds.Left;
Properties.Settings.Default.WinMainLocationY = this.RestoreBounds.Top;
Properties.Settings.Default.WinMain_size = new Size(this.RestoreBounds.Width, this.RestoreBounds.Height);
Properties.Settings.Default.WinMain_state = this.WindowState;
这个问题的一些答案显示了另一种使用 WinAPI 函数的方法 GetWindowPlacement
/ SetWindowPlacement
:
- .NET WPF Remember window size between sessions
添加 Environment.Exit(0)
已解决问题。我可以想象问题的原因是 Window.Closed-Handler 已达到两次。
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WinMainLocationX = this.Left; // ok
Properties.Settings.Default.WinMainLocationY = this.Top; // ok
Properties.Settings.Default.WinMain_size = new Size(this.Width, this.Height); // crucial setting
Properties.Settings.Default.WinMain_state = this.WindowState; // ok
Properties.Settings.Default.Save();
Environment.Exit(0);
}
我有一个-window WPF应用程序(Win8.1/.net4.7),Window.Closing-事件未处理,Window.Closed-事件处理为如下:
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WinMainLocationX = this.Left; // ok
Properties.Settings.Default.WinMainLocationY = this.Top; // ok
Properties.Settings.Default.WinMain_size = new Size(this.Width, this.Height); // crucial setting
Properties.Settings.Default.WinMain_state = this.WindowState; // ok
Properties.Settings.Default.Save();
}
我每天通过一个包含 C:\WINDOWS\system32\shutdown.exe /s /t 20
的批处理文件关闭应用程序(此时始终处于空闲状态),此后什么也没有。这样电脑就可以正常关机了。 shutdown.exe
的参数可以通过命令行输入shutdown /?
.
问题:每隔 7 或 8 天,window 大小就会以应用程序(早上启动后)看起来像这样的方式损坏:
如何保护我的应用程序设置免受 shutdown.exe
的干扰?
我认为问题出在应用程序 window 最小化时存储设置。在这种情况下,window 的宽度和高度将为 0。
您可以使用 window 的 RestoreBounds
属性 来获得独立于其当前状态的恢复大小:
Properties.Settings.Default.WinMainLocationX = this.RestoreBounds.Left;
Properties.Settings.Default.WinMainLocationY = this.RestoreBounds.Top;
Properties.Settings.Default.WinMain_size = new Size(this.RestoreBounds.Width, this.RestoreBounds.Height);
Properties.Settings.Default.WinMain_state = this.WindowState;
这个问题的一些答案显示了另一种使用 WinAPI 函数的方法 GetWindowPlacement
/ SetWindowPlacement
:
- .NET WPF Remember window size between sessions
添加 Environment.Exit(0)
已解决问题。我可以想象问题的原因是 Window.Closed-Handler 已达到两次。
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WinMainLocationX = this.Left; // ok
Properties.Settings.Default.WinMainLocationY = this.Top; // ok
Properties.Settings.Default.WinMain_size = new Size(this.Width, this.Height); // crucial setting
Properties.Settings.Default.WinMain_state = this.WindowState; // ok
Properties.Settings.Default.Save();
Environment.Exit(0);
}