Manage update in Application Exit

送分小仙女□ 提交于 2019-12-13 02:56:46

问题


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:


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



来源:https://stackoverflow.com/questions/32075084/manage-update-in-application-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!