在应用程序出口管理更新 [英] Manage update in Application Exit

查看:23
本文介绍了在应用程序出口管理更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Squirrel.Windows,我想在 我的 WPF 的应用程序退出处理程序使用此代码的应用程序:

Using Squirrel.Windows, I wanted to handle the update process in the Application Exit handler of my WPF application using this code:

Task.Run(async () =>
{
  using (var mgr = new UpdateManager(Settings.Default.UpdatePath))
  {
     var release = await mgr.UpdateApp();
     if (release != null && release.Version > Assembly.GetEntryAssembly().GetName().Version)
     {
        MessageBox.Show("Update applied");
     }
   }
});

如果我在启动时调用此代码,或在执行期间在事件处理程序上调用它,但不在像这样定义的应用程序退出事件处理程序中调用此代码:

This code works if I call it on startup, or on an event handler during execution, but not inside the Application Exit event handler defined like this:

app.xaml:

<Application 
   ...
      Exit="Application_Exit"
   ...

app.xaml.cs:

app.xaml.cs:

void Application_Exit(object sender, ExitEventArgs e)
   {
   ...
   }

这是 Squirrel.Windows 的限制吗?或者有什么特别的事情可以使用应用程序退出事件处理程序中提供的代码?

Is it a limitation of Squirrel.Windows? Or is there something special to do to use the code presented in the Application Exit event handler?

推荐答案

由于您正在创建一个立即运行的热门"Task,它将继续执行下一行代码.据推测,下一行代码是应用程序退出处理程序的结尾.如果您想防止这种情况发生,请执行以下操作:

Since you're creating a "hot" Task that is running immediately, it will continue to the next line of code. Presumably, that next line of code is the end of your application exit handler. If you want to prevent this from happening then do:

Task.Run(async () =>
{
  //do stuff here
}).Wait();

您可以通过向 Task.Wait

You may make use of timeout/cancel features by supplying appropriate arguments to Task.Wait

这篇关于在应用程序出口管理更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆