DethoRhyne
DethoRhyne

Reputation: 980

Finished Async download goes off twice instead of once

I have this code in my application that allows user to download the newest version of the application. When the application download is finished, it opens a prompt if user wants to open the file location to see the file.

However, the tool launches two Message boxes instead of only once. I'm not sure if I'm missing something.

private void BTN_GNV_MouseUp(object sender, MouseButtonEventArgs e)
{
    string URLDir = "http://shard.combatkingz.com/downloads/";
    string URLName = "DayZ Config Tweak tool v" + Properties.Settings.Default.AvailableVersion + ".exe";
    string URLFull = "";
    using (WebClient DLWC = new WebClient())
    {
        URLFull = URLDir + URLName;
        GlobalVars.DLPath = System.Environment.CurrentDirectory + "\\" + URLName;
        try
        {
            DLWC.DownloadFileAsync(new Uri(URLFull), GlobalVars.DLPath);
            DLWC.DownloadProgressChanged += DLWC_DownloadProgressChanged;
        }
        catch
        {
            MessageBox.Show("There was an error downloading the file.", GlobalVars.APPNAME, MessageBoxButton.OK, MessageBoxImage.Error);
#if DEBUG
#else
            AddDownloadToDB("Failed");
#endif
        }
    }
}
void DLWC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    PB_GNV.Width = (BTN_GNV.Width / 100) * e.ProgressPercentage;
    if (PB_GNV.Width == BTN_GNV.Width && e.TotalBytesToReceive == e.BytesReceived)
    {
        MessageBoxResult nav = MessageBox.Show("New version downloaded. Do you want to navigate to the folder?", GlobalVars.APPNAME, MessageBoxButton.YesNo, MessageBoxImage.Error);
        if (nav == MessageBoxResult.Yes)
        {
            string argument = @"/select, " + @GlobalVars.DLPath;
            System.Diagnostics.Process.Start("explorer.exe", argument);
#if DEBUG
#else
            AddDownloadToDB("Success");
#endif
        }
    }
}

Upvotes: 4

Views: 174

Answers (1)

Sign
Sign

Reputation: 1959

I suspect that the DownloadProgressChanged event is firing on receiving the last byte and on the file completed. Using the DownloadFileCompleted event should resolve the problem.

Upvotes: 3

Related Questions