kacalapy
kacalapy

Reputation: 10164

how to detect duration a WebClient.DownloadData() takes to complete?

how can i tell how long a:

objWebClient.DownloadData(strURL)

takes to complete?

i wish there was a property that contained this info but i couldn't find one...

Upvotes: 0

Views: 422

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503869

Just use a Stopwatch:

Stopwatch sw = Stopwatch.StartNew();
webClient.DownloadData(...);
sw.Stop();
Console.WriteLine("Download took {0}ms", sw.ElapsedMilliseconds);

If you're using synchronous APIs, it's really easy. It's trickier with asynchronous APIs, but you'd just need to pass around the stopwatch in your state. Again, that's pretty easy if you use an anonymous method or lambda expression for your event handler, as it can capture the local variable.

Upvotes: 2

Oded
Oded

Reputation: 499382

Use the StopWatch class - start it before the call and stop it after the call:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

objWebClient.DownloadData(strURL)

stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed; // ts now holds the duration

Upvotes: 2

Related Questions