【问题标题】:Reboot when required - c# and Windows Update需要时重新启动 - c# 和 Windows 更新
【发布时间】:2014-12-09 05:11:07
【问题描述】:

我正在构建一个 c# 控制台应用程序,它会自动查找更新、下载并安装它们。

我在安装部分使用这种方法:

        public static void InstallUpdates(UpdateCollection DownloadedUpdates)
    {
        UpdateSession UpdateSession = new UpdateSession();
        UpdateInstaller InstallAgent = UpdateSession.CreateUpdateInstaller() as UpdateInstaller;
        InstallAgent.Updates = DownloadedUpdates;

        //Starts a synchronous installation of the updates.
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa386491(v=VS.85).aspx#methods
        IInstallationResult InstallResult = InstallAgent.Install();

    }

根据this链接,我可以检查是否需要重新启动。我想要实现的是在 RebootRequiredBeforeInstallation 更改为 true 时立即完成系统重启。

我想过这样做,但那行不通,因为我不能使用 else 语句:

        while (!InstallAgent.RebootRequiredBeforeInstallation)
        {

        } else
        {
            // Reboot
        }

解决这个问题的正确方法是什么?

【问题讨论】:

  • 为什么不能在while 循环中使用if-else
  • @xxbbcc 起初我认为 if-else 语句会立即运行,而不是在一段时间后运行 - 但我越想越意识到在安装完成。对吗?

标签: c# windows api console windows-update


【解决方案1】:

这个怎么样:

while (!InstallAgent.RebootRequiredBeforeInstallation)
{
    // Install things
    // If the installer sets the RebootRequiredBeforeInstallation flag to
    // true, the while loop will terminate and you can reboot.
}

if (InstallAgent.RebootRequiredBeforeInstallation)
{
    // Reboot
}

【讨论】:

  • 我现在做了一些日志记录,似乎是 IInstallationResult InstallResult = InstallAgent.Install(); 之后的代码在安装完成之前不运行。所以我想 if-else 语句可以解决问题,不是吗?
  • @Erik 是的,应该。 同步安装就是这样 - 您调用安装程序来执行安装,您的代码将等待安装程序完成或失败,然后您的程序才能继续。
  • 我明白了。您对 Windows Update 相关代码了解很多吗?我在这里遇到了一些例外问题,我真的需要一些帮助,但我不能在明天之前发布。来晚了
  • @Erik 不,我以前从未使用过它。 :) 但是将您的问题作为个人问题发布,您可能会得到帮助。只需确保发布详细的问题,以便人们了解您正在尝试做什么以及出了什么问题。
  • 当然,我对这个网站的印象很好,所以我想这不会是一个问题:) 谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多