Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:40:09+00:00 2026-06-05T18:40:09+00:00

I added an event handler to the WebClient’s DownloadProgressChanged event, but it never seems

  • 0

I added an event handler to the WebClient’s DownloadProgressChanged event, but it never seems to fire. The file successfully downloads, but without having its progress updated.

public class DownloadFile
{
    private File file = null;

    public DownloadFile(File file)
    {
        this.file = file;
    }

    public void startDownloadThread()
    {
        Console.WriteLine("Starting Download : "+file.URL);

        var t = new Thread(() => DownloadThread(file));
        t.Start();
    }

    public Action<string> action_error_downloadFailed = Console.WriteLine;
    private void DownloadThread(File file) //Unnecessary argument but whatever ;D
    {
        try
        {
            string url = file.URL;
            string savepath = file.DestinationDir + "\\" + file.Filename;

            WebClient_B client = new WebClient_B();
            client.Proxy = null; //default to no proxy
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadFile(url, savepath);

            Console.WriteLine("Download finished :" + file.Filename);
        }
        catch (Exception ex)
        {
            if (action_error_downloadFailed != null)
                action_error_downloadFailed("Download failed :"+ex.Message);
        }
    }

    private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        try
        {
            if (file.TotalSize == 0)
                file.TotalSize = (int)e.TotalBytesToReceive;
            file.CurrentSize = (int)e.BytesReceived;

            Form_DownloadManager.rebuildQueue();

            Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
            (string)e.UserState,
            e.BytesReceived,
            e.TotalBytesToReceive,
            e.ProgressPercentage);
        }
        catch (Exception ex) { Console.WriteLine("client_DownloadProgressChanged error : "+ex.Message); }
    }
}

Output:

Starting Download : http://x.x.x/y/z.zip
'projectname.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x3b8c) has exited with code 0 (0x0).
Download finished :z.zip

I am using WebClient_B because I had to add useragent+cookiecontainer functionality to the WebClient class since my server kept rejecting the download request. The event never fired with the ‘standard’ WebClient class either, though. So that shouldn’t be the problem. But anyway; link to class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:40:10+00:00Added an answer on June 5, 2026 at 6:40 pm
    client.DownloadFile(url, savepath);
    

    You have to use the async version to download a file, currently you use the blocking, synchronous version.

    From the msdn docs for WebClient.DownloadProgressChanged:

    Occurs when an asynchronous download operation successfully transfers
    some or all of the data.

    In your case that would be:

    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
    client.DownloadFileAsync (url, savepath);
    

    Since your method does not directly return any result, this refactoring shouldn’t be a problem, note though that the download most likely has not been completed by the time the method returns to the caller.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does an event handler never get called if it's added within a loop
I have added an EventHandler for the Click-event to a picturebox but on runtime
I added an event handler to my code and it broke all access to
I added an event handler to my code and it broke all access to
I'm trying to remove a keydown event handler added by a scroller plugin into
I have added an event handler for itemOpening event for the tree component inside
How do I stop a click event handler added with jQuery's live method from
I added an event handler to redirect error_logger reports to syslog. The handler looks
I have created a new WPF application and added an event handler for Loaded
I've created some dynamic controls on page load and added an event handler to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.