Search code examples
c#wpfmultithreadingsquirrel.windows

Howto determine if all threads are done C#


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


Solution

  • 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.