Fragjacker / DoW-Mod-Manager

The original repository for the DoW Mod Manager application, which was made for the Dawn of War:tm: series.
MIT License
33 stars 7 forks source link

On-start autoupdate does not delete old executable anymore #21

Closed Fragjacker closed 4 years ago

Fragjacker commented 4 years ago

Issue:

@IgorTheLight When pressing Update on the start up update dialogue the app never fires the webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); event and thus the app does not close and remove itself, to start the newly downloaded version.

The update function from the AboutForm works just as intended.

TODO:

Fix the on start autoupdate function so that it properly removes the old app again.

Fragjacker commented 4 years ago

I found the culprit of it. It's the Thread() function which apparently prevents the event from happening. When I changed

if (settings[AUTOUPDATE] == 1)
{
    // Threads could work even if application would be closed
    new Thread(() =>
    {
        // Once all is done check for updates.
        DialogResult result = DownloadHelper.CheckForUpdates(silently: true);

        if (result == DialogResult.OK && settings[AOT_COMPILATION] == 1)
            settings[ACTION_STATE] = (int)Action.CreateNativeImage;
    }
    ).Start();
}

To

if (settings[AUTOUPDATE] == 1)
{
        // Once all is done check for updates.
    DialogResult result = DownloadHelper.CheckForUpdates(silently: true);

    if (result == DialogResult.OK && settings[AOT_COMPILATION] == 1)
        settings[ACTION_STATE] = (int)Action.CreateNativeImage;
}

It started to work again. However I am looking into how to make it work with the Thread since I'd like to keep the threaded version.

Fragjacker commented 4 years ago

Alright I fixed the problem. The solution was to put the downloader into a separate Thread too. I assume the main Thread terminated before the download could finish, hence the event never fired. Putting it into a separate Thread should guarantee that the event can fire properly once the download thread finished.

Here's the solution code that works.

// Start a new thread for the download part only.
new Thread(() =>
{
    // WebClient is more high level than HttpClient
    using (WebClient webClient = new WebClient())
    {
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

        try
        {
            webClient.DownloadFileAsync(new Uri(address), downloadPath);
            while (webClient.IsBusy) { Application.DoEvents(); }
            closeAndDelete = closeAndDeleteApplication;
        }
        catch (Exception ex)
        {
            ThemedMessageBox.Show(ex.Message, "Download Error:");
        }
    }
}).Start();
Fragjacker commented 4 years ago

Fixed in commit 2879007a7dcb1835fe3a32d496d74e779576c537, closed.

IgorTheLight commented 4 years ago

Thanks! I will sync it with my master branch. Threads do cause some issues sometimes ;-)

On Thu, Aug 20, 2020 at 8:56 PM Dennis S. notifications@github.com wrote:

Alright I fixed the problem. The solution was to put the downloader into a separate Thread too. I assume the main Thread terminated before the download could finish, hence the event never fired. Putting it into a separate Thread should guarantee that the event can fire properly once the download thread finished.

Here's the solution code that works.

// Start a new thread for the download part only.new Thread(() => { // WebClient is more high level than HttpClient using (WebClient webClient = new WebClient()) { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; //webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

    try
    {
        webClient.DownloadFileAsync(new Uri(address), downloadPath);
        while (webClient.IsBusy) { Application.DoEvents(); }
        closeAndDelete = closeAndDeleteApplication;
    }
    catch (Exception ex)
    {
        ThemedMessageBox.Show(ex.Message, "Download Error:");
    }
}

}).Start();

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Fragjacker/DoW-Mod-Manager/issues/21#issuecomment-677812029, or unsubscribe https://github.com/notifications/unsubscribe-auth/APM5F32ZBPEWJWSFKJO7AITSBVPWRANCNFSM4QGGNBKQ .