Aurionin
Aurionin

Reputation: 21

WebClient.DownloadProgressChanged doesn't fire

Doing some programming in Visual Studio with C# and I'm trying to download a file and link the progress to a ProgressBar. It worked perfectly fine when I did it in Visual Basic, but when I tried to do it in C#, the Client.DownloadProgressChanged event just doesn't fire.

System.Net.WebClient Client = new System.Net.WebClient();
Client.DownloadProgressChanged += Client_DownloadProgressChanged;
Client.DownloadFileAsync(new Uri(url), filepath + filename);
while (Client.IsBusy) { }

The only thing in Client_DownloadProgressChanged is some code that changes the value of a TextBox to "Test1" which I did just to see if it runs.

Upvotes: 2

Views: 1026

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

This:

while (Client.IsBusy) { }

Ties up the UI message loop. It keeps it consumed and doesn't allow any other messages to be processed. Meanwhile, the event is being queued on the UI thread, which is blocked.

If you comment that line and the download completes, you'll see you textbox update properly.

You should re-think what you're trying to do. If Client, consider moving the while loop to a background thread

Upvotes: 3

Related Questions