Manage update in Application Exit
Asked Answered
L

1

0

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?

Lurleen answered 18/8, 2015 at 14:21 Comment(4)
Do you wait for that task to finish? async in application exit smells.Anderegg
Try Task.Run( .......).Wait();. Specify a timeout if you want one.Sixty
@NETscape It works :) If you set an anwer, I'll accept it, thanks!Lurleen
Careful about doing just a naive wait here, there are some considerations that should be borne in mind, as laid out in Why Application.Exit event works even if handler is async void in WPF application lifecycle?Tetroxide
S
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

Sixty answered 18/8, 2015 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.