I found a thread (http://groups.google.com/group/
microsoft.publi c.dotnet.langua ges.csharp/browse_thread/thread/
b154f44522b5e08 3/11974fe21507faa e?
lnk=gst&q=webcl ient.CancelAsyn c&rnum=1#11974f e21507faae) about this
issue, but it can't solve my problem.
I simply put a progressbar (to show the progress of the downloading),
a lable (to show what's downloading), and a button (to cancel the
download) on a form. If I click cancel button while downloading, there
comes a TargetInvocatio nException. How should I avoid it, or at least
catch it?
And here is the code:
DownloadProgres sForm f=new DownloadProgres sForm(url, message)
if(f.ShowDialgo ()==DialogResul t.OK)
{
...
}
public partial class DownloadProgres sForm : Form
{
private readonly string url;
private byte[] data;
private WebClient client;
public DownloadProgres sForm(string url, string message)
{
InitializeCompo nent();
lblStatus.Text = message;
this.url = url;
}
public byte[] Data
{
get { return data; }
}
private void DownloadProgres sForm_Load(obje ct sender,
EventArgs e)
{
client = new WebClient();
client.Download ProgressChanged += DownloadProgres sChanged;
client.Download DataCompleted += DownloadDataCom pleted;
client.Download DataAsync(new Uri(url));
}
void DownloadDataCom pleted(object sender,
DownloadDataCom pletedEventArgs e)
{
if (e.Cancelled)
{
DialogResult =
System.Windows. Forms.DialogRes ult.Cancel;
}
else
{
DialogResult = System.Windows. Forms.DialogRes ult.OK;
}
data = e.Result;
Close();
}
void DownloadProgres sChanged(object sender,
DownloadProgres sChangedEventAr gs e)
{
progressBar1.Va lue = (int)(e.BytesRe ceived * 100 /
e.TotalBytesToR eceive);
}
private void btnCancel_Click (object sender, EventArgs e)
{
client.CancelAs ync();
}
}
microsoft.publi c.dotnet.langua ges.csharp/browse_thread/thread/
b154f44522b5e08 3/11974fe21507faa e?
lnk=gst&q=webcl ient.CancelAsyn c&rnum=1#11974f e21507faae) about this
issue, but it can't solve my problem.
I simply put a progressbar (to show the progress of the downloading),
a lable (to show what's downloading), and a button (to cancel the
download) on a form. If I click cancel button while downloading, there
comes a TargetInvocatio nException. How should I avoid it, or at least
catch it?
And here is the code:
DownloadProgres sForm f=new DownloadProgres sForm(url, message)
if(f.ShowDialgo ()==DialogResul t.OK)
{
...
}
public partial class DownloadProgres sForm : Form
{
private readonly string url;
private byte[] data;
private WebClient client;
public DownloadProgres sForm(string url, string message)
{
InitializeCompo nent();
lblStatus.Text = message;
this.url = url;
}
public byte[] Data
{
get { return data; }
}
private void DownloadProgres sForm_Load(obje ct sender,
EventArgs e)
{
client = new WebClient();
client.Download ProgressChanged += DownloadProgres sChanged;
client.Download DataCompleted += DownloadDataCom pleted;
client.Download DataAsync(new Uri(url));
}
void DownloadDataCom pleted(object sender,
DownloadDataCom pletedEventArgs e)
{
if (e.Cancelled)
{
DialogResult =
System.Windows. Forms.DialogRes ult.Cancel;
}
else
{
DialogResult = System.Windows. Forms.DialogRes ult.OK;
}
data = e.Result;
Close();
}
void DownloadProgres sChanged(object sender,
DownloadProgres sChangedEventAr gs e)
{
progressBar1.Va lue = (int)(e.BytesRe ceived * 100 /
e.TotalBytesToR eceive);
}
private void btnCancel_Click (object sender, EventArgs e)
{
client.CancelAs ync();
}
}
Comment