0

I have a WPF application that is updating itself on the background (via Squirrel.Windows) this is done by the following code:

var restartApp = false;
using (var mgr = new UpdateManager(@"http://wintst01:8282/unidealoffice/starter"))
{
    var re = await mgr.UpdateApp(DownloadProgress);
    if (re == null)
    {
        Debug.WriteLine("NULL");
    }
    else
    {
        MessageBox.Show($"Applicatie is bijgewerkt en dient herstart te worden\nNieuwe versie: {re.Version}", "Update");
        restartApp = true;
    }
}
if (restartApp)
{
    UpdateManager.RestartApp();
}

This code is in the OnStartup() of the App.xaml.cs This is an Async and Await call so it will be done in the background. Now i want to know if it is possible before i close my application i can see if this thread that is started is still running.. If so i cannot close the application but i want to hide the icon from taskbar and minimize the application. So i can still update when it finishes i want to close the application...

I hope someone can point me in the right direction.

UPDATE: Here is the full code of my OnStartup in the App.xaml.cs

protected override async void OnStartup(StartupEventArgs e)
    {
        this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        for (int i = 0; i < e.Args.Length; i++)
        {
            if (e.Args[i].ToLower() == "/ForceInstall".ToLower() || e.Args[i].ToLower() == "-ForceInstall".ToLower() || e.Args[i].ToLower() == "-F".ToLower() || e.Args[i].ToLower() == "/F".ToLower())
            {
                ApplicationConstants.Instance.ForceInstall = true;
            }
        }

        base.OnStartup(e);

        var restartApp = false;
        using (var mgr = new UpdateManager(@"http://wintst01:8282/unidealoffice/starter"))
        {
            var re = await mgr.UpdateApp(DownloadProgress);
            if (re == null)
            {
                Debug.WriteLine("NULL");
            }
            else
            {
                MessageBox.Show($"Applicatie is bijgewerkt en dient herstart te worden\nNieuwe versie: {re.Version}", "Update");
                restartApp = true;
            }
        }
        if (restartApp)
        {
            UpdateManager.RestartApp();
        }
    }

What i'm trying to do is Have Squirrel determine if there is an update they will automatically download all the necesary files and apply them after a restart of the application. But because this application is some kind of starter for another application that will check for updates for this application and install (unzip) them it will close itself after installation is completed and installed application is started. But if the starter application is still updating itself it cannot be closed and restarted.

So brief explanation with a simple visio drawing: The Visio drawing

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Jordy van Eijk
  • 2,718
  • 2
  • 19
  • 37
  • Just so I understand - is this code running in the background as a backgroundworker process? – Pedro G. Dias May 26 '16 at 08:48
  • @PedroG.Dias Squirrel is an auto-update library that checks a remote xml version file, downloads any new version installers and runs them. During this, a banner with a progress bar is displayed. – Panagiotis Kanavos May 26 '16 at 08:54
  • create a global boolean var named busy. set it to true before thread starts, false on return. Next set up an event hanlder for the app.cs onclosing event check the variable. If busy, do hiding etc. – JWP May 26 '16 at 08:55
  • @JohnPeters that's a very bad idea (global variables? locking?), when we are talking about asynchronous operations – Panagiotis Kanavos May 26 '16 at 08:55
  • First, you can always check the status of a Task. This isn't an option if you use an asynchronous event handler though. Second, installers typically run *after* a user closes the application himself. It's both polite and simpler to implement. – Panagiotis Kanavos May 26 '16 at 08:57
  • Good point so make a list of them instead using concurrentBag. But op never said anything about multiple threads. User can store each task as well and check status. – JWP May 26 '16 at 08:58
  • 2
    I can see only one task in the provided code. But as the title & the code suggest use `Task.WaitAll` method to wait for all tasks to complete (http://stackoverflow.com/questions/19849847/use-task-waitall-to-handle-awaited-tasks), if using thread use `ThreadPool` to determine it refer http://stackoverflow.com/questions/2520179/wait-until-all-threads-teminated-in-threadpool – G J May 26 '16 at 09:21
  • It is actually not a different thread, it is just an asynchronous call. using async and await. is it still possible then? – Jordy van Eijk May 26 '16 at 09:23
  • Post *your* code, specifically your OnStartup. Also explain *which* task you are trying to check. If you simply use `await runMyUpdateMethod()`, then there is no need to check anything. Only the update method will be running. There's no need to wait for that task to complete before restarting, since it's going to restart the application by itself – Panagiotis Kanavos May 26 '16 at 09:36
  • @PanagiotisKanavos i updated the question with a little diagram of what it should do – Jordy van Eijk May 26 '16 at 10:53

1 Answers1

1
private Task updateTask;

protected override async void OnStartup(StartupEventArgs e)
{
    updateTask = StartupAsync(e);
}

private Task StartupAsync(StartupEventArgs e)
{
    this.Dispatcher.UnhandledException += Dispatcher_UnhandledException;
    for (int i = 0; i < e.Args.Length; i++)
    {
        if (e.Args[i].ToLower() == "/ForceInstall".ToLower() || e.Args[i].ToLower() == "-ForceInstall".ToLower() || e.Args[i].ToLower() == "-F".ToLower() || e.Args[i].ToLower() == "/F".ToLower())
        {
            ApplicationConstants.Instance.ForceInstall = true;
        }
    }

    base.OnStartup(e);

    var restartApp = false;
    using (var mgr = new UpdateManager(@"http://wintst01:8282/unidealoffice/starter"))
    {
        var re = await mgr.UpdateApp(DownloadProgress);
        if (re == null)
        {
            Debug.WriteLine("NULL");
        }
        else
        {
            MessageBox.Show($"Applicatie is bijgewerkt en dient herstart te worden\nNieuwe versie: {re.Version}", "Update");
            restartApp = true;
        }
    }
    if (restartApp)
    {
        UpdateManager.RestartApp();
    }
}

In the closing event you can now await the updateTask and / or check the Status of the Task.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74