Reputation: 34006
basically i would like to do something like this:
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += (sender, args) =>
{
progress = (float) args.BytesReceived / (float) args.TotalBytesToReceive;
};
wc.DownloadFile(new Uri(noLastSegment + file), path);
}
this doesnt work, because the progress is only fired for asynchronous downloads like DownloadFileAsync.
Upvotes: 0
Views: 419
Reputation: 40788
Usually you've got some other thread like a UI thread if you can show progress, however maybe you've got a console app or something. You can easily use some kind of wait handle and set it when the download completes.
using (var completedEvent = new ManualResetEventSlim(false))
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += (sender, args) =>
{
completedEvent.Set();
};
wc.DownloadProgressChanged += (sender, args) =>
{
progress = (float) args.BytesReceived / (float) args.TotalBytesToReceive;
};
wc.DownloadFileAsync(new Uri(noLastSegment + file), path);
completedEvent.Wait();
}
Upvotes: 4