webclient timeout

M

Macneed

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 = client.DownloadData("http://www.microsoft.com");
HTMLstring = Encoding.ASCII.GetString(HP);


is it possible to add a timeout timer for this code, e.g. 10s
or i must use another class, like webrequest to doing so?

Thanks for your help!!
 
P

Patrick Steele

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 = client.DownloadData("http://www.microsoft.com");
HTMLstring = Encoding.ASCII.GetString(HP);


is it possible to add a timeout timer for this code, e.g. 10s
or i must use another class, like webrequest to doing so?

You could use DownloadDataAsync to run the command asynchornously (while
other code is running -- like a progress dialog that will wait 10
seconds). If the "DownloadDataCompleted" event is raised within the 10
seconds, you're good. Otherwise, you can use the CancelAsync method and
let the user know the request timed out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top