Squirrel.windows: Example that uses RestartApp?

Created on 10 Feb 2015  路  5Comments  路  Source: Squirrel/Squirrel.Windows

Does someone have an example that uses UpdateManager.RestartApp() they would be willing to share? If so, I'd be most grateful. I'd like the app to restart immediately after downloading the update. This is my first attempt:

    private async void ScheduleAppUpdates(Object o)
    {
        var appName = Assembly.GetExecutingAssembly().GetName().Name;
        var appUpdateCheckLocation = Settings.Default.AppUpdateCheckLocation;
        using (var mgr = new UpdateManager(appUpdateCheckLocation, appName, FrameworkVersion.Net45))
        {
            SquirrelAwareApp.HandleEvents(
                onInitialInstall: v => mgr.CreateShortcutForThisExe(),
                onAppUpdate: (v) =>
                             {
                                 System.IO.File.WriteAllText(
                                     @"C:\Users\don\AppData\Local\success.txt", 
                                     @"onAppUpdate was called.");
                                 mgr.CreateShortcutForThisExe();
                                 UpdateManager.RestartApp();
                             },
                onAppUninstall: v => mgr.RemoveShortcutForThisExe());

            try
            {
                UpdateInfo updateInfo = await mgr.CheckForUpdate();
                if (updateInfo.FutureReleaseEntry != null)
                {
                    await mgr.UpdateApp();
                }
            }
            catch (Exception ex)
            {
                ExceptionService.ReportException(ex);
            }
        }
    }

While the update is being detected and downloaded, the shortcut isn't being changed, the text file isn't being created, and the app isn't restarting. Install and uninstall are working as expected.

old-version

Most helpful comment

@don-smith @willdean I'm going to close this out as over four years have passed since the last comment and it's not clear to me which versions were used at the time, but feel free to open a new issue with more information if this is still something you've encountered.

All 5 comments

Your problem is that you need to put the block around handling Squirrel events in your startup routine, as early as possible.

Thanks Paul. That makes a lot of sense. It turns out the problem I had was probably a combination of your point and the fact I wasn't creating/releasing my single-instance mutex correctly. Now it looks like the event handler is being called, but the app still isn't restarting. After the update comes down, the app isn't shut down, but is being started again, which my mutex logic prevents. I'm creating the mutex in Main() and releasing it in the App.OnExit event. Seem reasonable? Is there anything that would prevent RestartApp() from closing the app? Thanks again.

I've looked into this some more, and I think the issue is one of two things. Either the app is being shutdown before Update.exe has time to grab the PID, and is therefore unable to restart the app. Or I'm doing something wrong in the app.

I've created a sample app wherein I've commented out the last line of UpdateManager.RestartApp() (which is Environment.Exit(0);) and done an await Task.Delay(1000); before having the app close itself down. This works without fail.

I would be grateful to get some validation and/or guidance on this approach. Cheers!

@locksmithdon I've also been wrestling with this, because I was working with the nuget binaries which don't have that new delay in, so don't restart reliably on this machine. I have fiddled around and added a mechanism to pass the PID from the outgoing app to Update.exe, because adding just an 'open loop' delay makes me uncomfortable, as I don't see a way to safely determine how long it should be.

Having done all this PID passing, I now can't actually see any reason why Update.exe couldn't just decide that the failure of GetParentProcessId is a sure sign that the parent has finished and press on with the restart - at the moment it throws a Win32Exception in this circumstance and so fails. I suspect there's something I'm missing here, but I don't know what.

@don-smith @willdean I'm going to close this out as over four years have passed since the last comment and it's not clear to me which versions were used at the time, but feel free to open a new issue with more information if this is still something you've encountered.

Was this page helpful?
0 / 5 - 0 ratings