WebClient and Events
-
Ok Guys I'm facing a strange problem here, and I dunno if its a bug or I am doing something wrong. Here's the code:
using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;namespace ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
var webClient = new WebClient();//var progressBar = new ProgressBar(); webClient.DownloadProgressChanged += (o, e) => Debug.WriteLine(e.ProgressPercentage); webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed."); Debug.WriteLine("Starting download..."); webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso"); Console.Read(); } }
}
Everything works fine like this, events are raised normally. Now try to uncomment the progressbar initialization and events will stop to be fired (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens) even if the download proceeds normally. Maybe is just me or maybe I have convinced myself this code is right that I cant no longer see where I epic fail. Anyone having the same problem? Thanks in advance. Matt
-
Ok Guys I'm facing a strange problem here, and I dunno if its a bug or I am doing something wrong. Here's the code:
using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;namespace ConsoleApplication
{
internal class Program
{
private static void Main(string[] args)
{
var webClient = new WebClient();//var progressBar = new ProgressBar(); webClient.DownloadProgressChanged += (o, e) => Debug.WriteLine(e.ProgressPercentage); webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed."); Debug.WriteLine("Starting download..."); webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso"); Console.Read(); } }
}
Everything works fine like this, events are raised normally. Now try to uncomment the progressbar initialization and events will stop to be fired (I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens) even if the download proceeds normally. Maybe is just me or maybe I have convinced myself this code is right that I cant no longer see where I epic fail. Anyone having the same problem? Thanks in advance. Matt
Hi, this looks strange! I'd guess your problem in the example is Console.Read()... When I tried your code in an windows forms project there was no problem. Can you show your orignal code
Teuz wrote:
(I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens)
-
Hi, this looks strange! I'd guess your problem in the example is Console.Read()... When I tried your code in an windows forms project there was no problem. Can you show your orignal code
Teuz wrote:
(I noticed the problem with the progress bar because I needed to add a progress bar dynamically to the form, dunno if there are other scenarios where this happens)
Thank you for the reply. How did you managed to make it work in a windows form project? This code acts exactly like the console one, no event are fired but the file is downloaded:
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}public void CheckUpdate(object arg) { // This simulates my update process DownloadFile(); Thread.Sleep(5000); Debug.WriteLine("Update completed"); } public void DownloadFile() { var webClient = new WebClient(); var progressBar = new ProgressBar(); AddControl(progressBar); webClient.DownloadProgressChanged += (o, e) => { Debug.WriteLine(e.ProgressPercentage); progressBar.Value = e.ProgressPercentage; }; webClient.DownloadFileCompleted += (o, e) => Debug.WriteLine("Download completed."); Debug.WriteLine("Starting download..."); webClient.DownloadFileAsync(new Uri("http://cdimage.debian.org/debian-cd/5.0.4/i386/iso-cd/debian-504-i386-netinst.iso"), "debian-504-i386-netinst.iso"); } private void button1\_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(CheckUpdate); } private void AddControl(Control control) { if (flowLayoutPanel1.InvokeRequired) flowLayoutPanel1.BeginInvoke(new MethodInvoker(() => AddControl(control))); else flowLayoutPanel1.Controls.Add(control); } }
}
This is also quite similar the code I'm actually using in my project. I forgot to mention that for this project I'm using NET 2.0 so maybe this can work differently in later versions.