Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to show progress using DownloadDataAsync

Trying to show progress using DownloadDataAsync

Scheduled Pinned Locked Moved C#
mobilevisual-studioquestionannouncement
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_rm
    wrote on last edited by
    #1

    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) + " ]");
    }

    A A 2 Replies Last reply
    0
    • S steve_rm

      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) + " ]");
      }

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • A Abhijit Jana

        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

        S Offline
        S Offline
        Steve1_rm
        wrote on last edited by
        #3

        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,

        L L 2 Replies Last reply
        0
        • S Steve1_rm

          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,

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Are you by any chance downloading from a passive FTP-server? Just found this in the documentation;

          Documentation says[^]:

          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 :)

          S 1 Reply Last reply
          0
          • L Lost User

            Are you by any chance downloading from a passive FTP-server? Just found this in the documentation;

            Documentation says[^]:

            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 :)

            S Offline
            S Offline
            Steve1_rm
            wrote on last edited by
            #5

            Hello, I am downloading from a http web server using IIS; Thanks,

            L 1 Reply Last reply
            0
            • S Steve1_rm

              Hello, I am downloading from a http web server using IIS; Thanks,

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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"

              1 Reply Last reply
              0
              • S Steve1_rm

                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,

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.


                1 Reply Last reply
                0
                • S steve_rm

                  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) + " ]");
                  }

                  A Offline
                  A Offline
                  Alan N
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups