2

Edit: The linked "same question" seems to be for regular windows forms apps, not windows RT which has a limited .NET library.

I am developing a Windows App that has the ability to upload a zip file to a server with progress. I am using a custom Stream class to handle the progress updates. The stream class is as follows (snippet):

 public class RTMStreamWithProgress : System.IO.Stream
    {
        private readonly System.IO.Stream file;
        private readonly long length;
        private long bytesRead;

        public event EventHandler<decimal> ProgressChanged;

        public RTMStreamWithProgress(System.IO.Stream file)
        {
            this.file = file;
            length = file.Length;
            bytesRead = 0;
            if (ProgressChanged != null)
            {

                ProgressChanged(this, 0);
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int result = file.Read(buffer, offset, count);
            bytesRead += result;
            if (ProgressChanged != null)
            {
                ProgressChanged(this, (decimal)((double)bytesRead/(double)length));
            }
            return result;
        }

        //more stuff
    }

And then in my app, I use this to try to upload a file to a server here:

//Initialize updateable stream
RTMStreamWithProgress progStream = new RTMStreamWithProgress(zipAsStream);
progStream.ProgressChanged += upload_ProgressChanged;

private async void upload_ProgressChanged(object sender, decimal e)
{
    Debug.WriteLine((e * 100).ToString());
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        HubDataContext globalContext = (HubDataContext) Application.Current.Resources["GlobalDataContext"];
        globalContext.SubProgressValue = Convert.ToDouble(e * 100);
    });

}

The ProgressBar has a its Value bound to the SubProgressValue property that I am updating. My issue is that I can see in the console via Debug.Writeline that the stream is being read very fast and I get very quick updates multiple times per second, such as:

0.0086728887868017600
0.1474391093756300
2.3676986387968800
4.5879581682181300
6.8082176976393800
//so on and so forth...

But the progress bar is only updating like 2 or 3 times during the entire upload process, and the value it updates to is seemingly random (but still one of the values that was reported by upload_ProgressChanged. The reason I have the binding update inside of the this.Dispatcher... is because it wouldn't work without that, causing crashes relating to trying to access the UI from a separate thread.

How can I get the progress bar to update just as fast and as often as I can seeing with Debug.WriteLine, while keeping the Async-Await design?

user2357446
  • 656
  • 6
  • 25
  • Without [a good, _minimal_, _complete_ code example](http://stackoverflow.com/help/mcve) it's not possible to say for sure what the problem is. But, have you tried using a `Stopwatch` to track how often you are raising the `ProgressChanged` event, and reducing the frequency to something reasonable, like once per second, or 4-5 times per second at most? If you fill the dispatcher queue with a large number of updates, it may just not be able to keep up. – Peter Duniho Feb 24 '15 at 05:08

0 Answers0