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.