Manage update in Application Exit

2019-09-11 09:32发布

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:

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

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?

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-11 10:05

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();

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

查看更多
登录 后发表回答