【问题标题】:"shutdown.exe" destroys application settings“shutdown.exe”破坏应用程序设置
【发布时间】:2018-10-11 13:44:02
【问题描述】:

我有一个单窗口WPF应用程序(Win8.1 / .net4.7),Window.Closing-Event未处理,Window.Closed-Event处理如下:

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 天,窗口大小就会损坏,应用程序(在早上启动后)如下所示:

如何保护我的应用程序设置免受shutdown.exe 的干扰?

【问题讨论】:

  • 从设置中定位窗口的代码在哪里?
  • 您可以尝试将代码移动到Application.Exit 事件的事件处理程序。它应该在应用程序关闭或 Windows 会话结束时触发。但不能保证您的数据在会话终止之前始终正确保存。
  • 这台机器需要更好的反恶意软件产品或不应该自动关闭。您可以在 .bat 文件中使用 taskkill.exe,这样产品就有足够的时间来完成 XML 扫描。
  • @mm8:感谢您的建议,我没有检查它,因为在此期间一个班轮已经解决了这个问题。

标签: c# wpf batch-file shutdown application-settings


【解决方案1】:

我认为问题在于在应用程序窗口最小化时存储设置。在这种情况下,窗口的宽度和高度将为 0。

您可以使用窗口的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 的另一种方法:

【讨论】:

  • 同时,我已经在 Closing-Handler 中使用 RestoreBounds.WidthRestoreBounds.Height 尝试了您的建议并且它有效。我更喜欢这个解决方案。
【解决方案2】:

添加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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2016-05-16
    相关资源
    最近更新 更多