WebClient.DownloadFileAsync method

Hi, I wrote a program that need to download on demend a file from a given url. I built a download form that gets the url and the file path to save and it shows a progress bar. The path and filename are given to the form on the c'tor and the download itself starts when a method is being called.
Here is the code: (It is written in VS2005 beta 2)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace TechnoTask.View {
public partialclass DownloadWin : Form {
private String url;

private String filepath;

private String directory;

private WebClient client;

public DownloadWin(String link,String path, String file) {
InitializeComponent();
url = link;
filepath = path + file;
directory = path;
urlLable.Text = "Downloading: " + link;
fileNameLable.Text = "Destination: " + file;
this.Visible =true;
}

void client_DownloadFileCompleted(object sender,
AsyncCompletedEventArgs e) {
cancel.Text = "Close";
openDir.Enabled =true;
}

void client_DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e) {
downloadProgressBar.Value = e.ProgressPercentage;
}

privatevoid cancel_Click(object sender, EventArgs e) {
client.CancelAsync();
client.Dispose();
this.Dispose();
}

publicvoid startDownload() {
client =new WebClient();
client.DownloadProgressChanged +=new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted +=new AsyncCompletedEventHandler(client_DownloadFileCompleted);
try {
client.DownloadFileAsync(new Uri(url), filepath);
}catch (Exception) {
File.Delete(filepath);
client.Dispose();
this.Dispose();
}
}

privatevoid openDir_Click(object sender, EventArgs e) {
Process proc =new Process();
proc.StartInfo.FileName = "explorer";
proc.StartInfo.Arguments = directory;
proc.Start();
}
}
}

The problem is that the download stucks the viewing of the form for 4 seconds or so and what I get is a semi complete form and the whole program is stuck (other forms also) after the initial stuck the program continue to function and the form is viewed. I don't mind the program halt but I want the form to finish it's drawing before it is stuck (fixing the halt would be even better).
I'm not using threads but the DownloadFileAsync should not stop the program (or is it?).
If you have suggestions please tell me.

[4141 byte] By [Raananan] at [2008-2-7]
# 1
I believe that your problem is with the client_DownloadProgressChanged handler.
DownloadFileAsync should be running on a separate thread, and you can't update a control from a separate thread.

So, if you use downloadProgressBar's Invoke method, I suspect that will resolve your issue.

RichardR at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
Richard is correct in that your client_DownloadProgressChanged handler could very well be called on a different thread and thus the Invoke method is need on a WinForms control if you are trying to manipulate the control from a thread other than the one that created the control.
MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3

Hi all

I'm quite noob, but i'm having the same problem...

Can anyone post an example of this solution please?

Thanks

PedroSimao at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

Hello, you must to put this line:

CheckForIllegalCrossThreadCalls = false;

Bye!

AlanGrosz at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified