#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    392
    Rep Power
    24

    WPF download progress bar


    I have a download progress bar in a WPF application but it initially won't update. It usually works second time around once MyFile.zip already exists. Any reason for unexpected progress bar behaviour?

    Code:
    private async Task DownloadFile()
            {
                _dlFilePath = _solution.TempDownloadDirectory + "MyFile.zip";
    
                using (var client = new WebClient())
                {
                    client.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
                    client.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; WOW64; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0");
                    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
                    client.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadComplete);
                    await client.DownloadFileTaskAsync(new Uri(_solution.SolutionDownloadUrl), _dlFilePath);
    
                    while (client.IsBusy)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }
    
            private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                this.Dispatcher.Invoke(() =>
                {
                    pbDownloadStatus.Value = e.ProgressPercentage;
                });
            }
    
            private void wc_DownloadComplete(object sender, AsyncCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    OutputErrorCloseWindow(e.Error);
                }
            }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2017
    Posts
    2
    Rep Power
    0
    Maybe you can make the progressbar continue even after the download is complete, or do a partial download of the file, see how long that takes, and extrapolate out for a guess of how long it will take to download the entire file.

IMN logo majestic logo threadwatch logo seochat tools logo