问题
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