473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

WebClient.CancelAsync() and TargetInvocationException

I found a thread (http://groups.google.com/group/
microsoft.public.dotnet.languages.csharp/browse_thread/thread/
b154f44522b5e083/11974fe21507faae?
lnk=gst&q=webclient.CancelAsync&rnum=1#11974fe2150 7faae) 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 TargetInvocationException. How should I avoid it, or at least
catch it?

And here is the code:

DownloadProgressForm f=new DownloadProgressForm(url, message)
if(f.ShowDialgo()==DialogResult.OK)
{
...
}
public partial class DownloadProgressForm : Form
{
private readonly string url;
private byte[] data;
private WebClient client;

public DownloadProgressForm(string url, string message)
{
InitializeComponent();
lblStatus.Text = message;
this.url = url;
}

public byte[] Data
{
get { return data; }
}

private void DownloadProgressForm_Load(object sender,
EventArgs e)
{
client = new WebClient();
client.DownloadProgressChanged += DownloadProgressChanged;
client.DownloadDataCompleted += DownloadDataCompleted;
client.DownloadDataAsync(new Uri(url));
}

void DownloadDataCompleted(object sender,
DownloadDataCompletedEventArgs e)
{
if (e.Cancelled)
{
DialogResult =
System.Windows.Forms.DialogResult.Cancel;
}
else
{
DialogResult = System.Windows.Forms.DialogResult.OK;
}
data = e.Result;
Close();
}

void DownloadProgressChanged(object sender,
DownloadProgressChangedEventArgs e)
{
progressBar1.Value = (int)(e.BytesReceived * 100 /
e.TotalBytesToReceive);
}

private void btnCancel_Click(object sender, EventArgs e)
{
client.CancelAsync();
}
}

Aug 15 '07 #1
1 6124
I don't know what's better than Application.ThreadException event to
catch this exception. But it works strangely: if in the event I pop a
MessageBox, the application continues to run, If I don't, it will quit
immediately! I believe it should be my decision to make whether or not
exit the application, but not Application or whatever.
What's wrong with my code?

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

Application.ThreadException +=
Application_ThreadException;
Application.Run(new MainForm());
}

static void Application_ThreadException(object sender,
ThreadExceptionEventArgs e)
{
//logger.Error(e.Exception.Message);
//if the statement below is not present, application will
exit at once!
//but if it's here, application continues to run.
MessageBox.Show(e.Exception.Message);
}

Aug 15 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dudi | last post by:
Hi, very puzzling... I have one perfectly working solution, in which I use a a.dll which has a class A. that class loads data from the database and for each row deserialize the cell into class A1....
1
by: Greg Patrick | last post by:
My problem: I load an some assemblies (strong named) from a byte array using Assembly.Load(byte). They load fine. But one one of them is actually accessed, it's referenced assemblies can't be...
6
by: A.M-SG | last post by:
Hi, I have an aspx page at the web server that provides PDF documents for smart client applications. Here is the code in aspx page that defines content type: Response.ContentType =...
6
by: Giovanni | last post by:
Hi Guys, Really strange problem I am experiencing... I have setup a virtual directory with Read Only permissions on an ISA/IIS server (SBS 2003). In that virt. dir., I placed 1 file...
1
by: Mad Scientist Jr | last post by:
For some reason I can't get a WebClient to access an outside URL from behind our firewall. The code works when it runs outside the firewall. I turned on windows authentication in the web.config...
0
by: Neil Moss | last post by:
Hi all I have a SoapExtension that is monitoring various aspects of all web service activity from my WinForms client. From within the extension, is there any way that I can detect that an...
2
by: Zytan | last post by:
I just got a TargetInvocationException from a WebBrowser control, after calling WebBrowser.Document.Write(text), which (internally) calls System.Windows.Forms.HtmlDocument.Write(String text),...
1
by: Macneed | last post by:
i am a newbie, i want to write a code to download data from web using webclient string HTMLstring = ""; WebClient client = new WebClient(); Byte HP =...
0
by: JamesB | last post by:
I have a bit of code whereby I use the OpenReadAsync method to open a website to ensure it is responding ok. After the program has been running for some time, this method never returns to my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.