Trying to show progress using DownloadDataAsync
-
Hello, VS 2008 SP1 I am using the DownloadDataAysnc. But the ProgressChanged event doesn't show progress until after the data has been downloaded. Even when I try and download a data which is contained in a big file. The programs remains responsive so I know it is doing something. However, it is when the progress has completed that the progressChanged event fires. I known this as the progressChanged and the DownloadDataCompleted fire immediately after each other. However, they should be a pause as the file is quite big. This is the code snippet I am currently using. And the output below. What is strange the e.progresspercentage is 100%. And seems to get called twice. Many thanks for any advise, Results:
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]private void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
DownloadFiles df = new DownloadFiles();
string webServerURL = df.webServerPath;wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1"); }
void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
progressBar1.Value = e.ProgressPercentage;
Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
}void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
byte[] result = e.Result;
Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
} -
Hello, VS 2008 SP1 I am using the DownloadDataAysnc. But the ProgressChanged event doesn't show progress until after the data has been downloaded. Even when I try and download a data which is contained in a big file. The programs remains responsive so I know it is doing something. However, it is when the progress has completed that the progressChanged event fires. I known this as the progressChanged and the DownloadDataCompleted fire immediately after each other. However, they should be a pause as the file is quite big. This is the code snippet I am currently using. And the output below. What is strange the e.progresspercentage is 100%. And seems to get called twice. Many thanks for any advise, Results:
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]private void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
DownloadFiles df = new DownloadFiles();
string webServerURL = df.webServerPath;wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1"); }
void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
progressBar1.Value = e.ProgressPercentage;
Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
}void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
byte[] result = e.Result;
Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
}steve_rm wrote:
But the ProgressChanged event doesn't show progress until after the data has been downloaded.
Yes, this because of UI thread is different from you background thread. You have to use Background Worker for resolve is Issue. Where you can so Progressbar continuing along with dowloading. Here is an sample example of Background worker, Using the BackgroundWorker Component in .NET 2 applications Hope this will help you :-D
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you. View My Latest Article
-
steve_rm wrote:
But the ProgressChanged event doesn't show progress until after the data has been downloaded.
Yes, this because of UI thread is different from you background thread. You have to use Background Worker for resolve is Issue. Where you can so Progressbar continuing along with dowloading. Here is an sample example of Background worker, Using the BackgroundWorker Component in .NET 2 applications Hope this will help you :-D
Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you. View My Latest Article
Hello, Thanks for the reply. According to this msdn. http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx The DownloadDataAysnc does show progress. Not sure why mine doesn't. Many thanks for any other suggestions,
-
Hello, Thanks for the reply. According to this msdn. http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx The DownloadDataAysnc does show progress. Not sure why mine doesn't. Many thanks for any other suggestions,
Are you by any chance downloading from a passive FTP-server? Just found this in the documentation;
If the server does not send the size of the downloaded file (such as in the case of a passive FTP connection), ProgressPercentage may always be zero.
What happens if you put a breakpoint in the
DownloadProgressChangedEventHandler
? Does the break get hit during download?I are Troll :)
-
Are you by any chance downloading from a passive FTP-server? Just found this in the documentation;
If the server does not send the size of the downloaded file (such as in the case of a passive FTP connection), ProgressPercentage may always be zero.
What happens if you put a breakpoint in the
DownloadProgressChangedEventHandler
? Does the break get hit during download?I are Troll :)
-
Can you verify (using a new project, just for testing) whether this adapted code works as expected?
static void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
string webServerURL =
@"http://download.wikimedia.org/simplewiktionary/20090801/simplewiktionary-20090801-pages-articles.xml.bz2";wbCheckUpdates.DownloadDataAsync(new Uri(webServerURL));
}
static void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Download progres: " + e.ProgressPercentage);
}static void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
// byte[] result = e.Result;
Console.WriteLine("Completed");
}static void Main(string[] args)
{
UpdateAvailable();
Console.ReadKey();
}"The human mind ordinarily operates at only ten 10% of its capacity, the rest is overhead for the operating system"
-
Hello, Thanks for the reply. According to this msdn. http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadprogresschanged.aspx The DownloadDataAysnc does show progress. Not sure why mine doesn't. Many thanks for any other suggestions,
Hi, what Abhijit Jana told you is correct.
Steve1_rm wrote:
The DownloadDataAysnc does show progress.
No. What the MSDN page says is "(This event) occurs when an asynchronous download operation successfully transfers some or all of the data." So it tells you some progress has been made, it does however run on some thread (most probably a ThreadPool thread) different from the main thread, and hence it is not allowed to touch any Control. You may want to read some of my articles: - http://www.perceler.com/articles1.php?art=asyncpool1[^] - http://www.perceler.com/articles1.php?art=crossthreads1[^] :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hello, VS 2008 SP1 I am using the DownloadDataAysnc. But the ProgressChanged event doesn't show progress until after the data has been downloaded. Even when I try and download a data which is contained in a big file. The programs remains responsive so I know it is doing something. However, it is when the progress has completed that the progressChanged event fires. I known this as the progressChanged and the DownloadDataCompleted fire immediately after each other. However, they should be a pause as the file is quite big. This is the code snippet I am currently using. And the output below. What is strange the e.progresspercentage is 100%. And seems to get called twice. Many thanks for any advise, Results:
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]private void UpdateAvailable()
{
WebClient wbCheckUpdates = new WebClient();
wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
wbCheckUpdates.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wbCheckUpdates_DownloadDataCompleted);
DownloadFiles df = new DownloadFiles();
string webServerURL = df.webServerPath;wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1"); }
void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
progressBar1.Value = e.ProgressPercentage;
Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
}void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
byte[] result = e.Result;
Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
}Hi, I've got a few comments on the code although it does look essentially correct and I can't see anything that is obviously broken. 1) In the completion handler there is no check for success and without that it is not possible to draw any conclusions about other parts of the operation. 2) In the example given the results string is very short, only 6 characters, and so it is quite possible that the first progress notification is 100%. Log the e.BytesReceived and e.TotalBytesToReceive values to get a bit more info here. 3) Bear in mind that UpdateAvailable will return before the download is complete as DownloadDataAsync is non blocking. This leaves the WebClient object eligible for garbage collection and that could cause problems. Example code for checking successful completion:
private void DownloadDataCallback (Object sender, DownloadDataCompletedEventArgs e)
{
// If the request was not canceled and did not throw
// an exception, get the result.
if (e.Error == null && !e.Cancelled)
{
byte[] data = e.Result;
}
else
{
//error or cancelled
}
}I've read the other responses and it has been suggested that you should not update the progress bar directly due to cross thread issues. I disagree as WebClient operates in the same way as BackgroundWorker with event notifications marshaled to the UI thread. This means your code is safe without using Invoke on the controls. Alan.