ProgressStream: A Stream with Read and Write events


ProgressStream: A Stream with Read and Write events

In my experience with WCF I’ve implemented several solutions that leverage the message streaming capabilities present in the framework. Specifically in a client/server scenario when either side needs to transfer a file to the other side it is really convenient to be able to just pass a FileStream to a WCF service call and have the server read bytes from it and write them to disk as it pleases.

However, in this exact scenario, while the server can monitor and control how the bytes are read from the stream and track the progress, the client has no inherent way of determining how much of the file has been read across the network by the server. It would be really useful if the client could track this upload progress and display a progress bar or percentage to the user.

Well, I’ve created one of probably many possible solutions to this dilemma. I’ve written a class that inherits from Stream and exposes some events that are raised when bytes are read or written to/from the stream. These events provide information for how many bytes were read from the stream, what the current position is in the stream, and what the stream’s total length is.

The ProgressStream, as I call it, accepts any Stream object and encapsulates it, forwarding all regular stream calls to the underlying object. With this you can use the ProgressStream in conjunction with any other stream in .Net and leverage the added functionality.

Well enough explanation, here’s the code:

ProgressStream.cs (C#)
ProgressStream.vb (VB.Net)

And here’s a simple example in C# of how to use it:

using System;
using System.IO;
using CGS;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            var test = File.OpenRead(@"C:\Test\Test.doc");
            var pStream = new ProgressStream(test);
            pStream.BytesRead += 
                new ProgressStreamReportDelegate(pStream_BytesRead);
            int bSize = 4320;
            byte[] buffer = new byte[bSize];
            while (pStream.Read(buffer, 0, bSize) > 0) { }
        static void pStream_BytesRead(object sender, 
                                      ProgressStreamReportEventArgs args)
                string.Format("{2} bytes moved | {0} of {1} total", 

About the author

Mel administrator

6 Comments so far

WhatareliefPosted on4:51 am - Mar 6, 2018

This simple idea rescued me from days of Azure vs UWP hell. MS recommended solutions for Azure BLOB downloads were working in non-UWP projects but failing in UWP. Now I have progress reporting using the technique on this page.

AllanPosted on1:54 pm - Jan 30, 2018

So is there any way to adapt this for streaming a file to a web server? Currently I am sending files, anywhere from 90Mb to 140Mb, and would like to get a progress status. I tried to adapt your code but I’m doing something wrong. Right now the sending of the file look like this:

Dim stream As Stream = request.GetRequestStream
stream.Write(myFile, 0, myFile.Length)

Mike PoslusznyPosted on2:44 pm - Apr 6, 2015

Five years after you created it we still need this because .NET doesn’t do stream eventing (at least not that I found). Thank you for creating and sharing this, it’s just what I needed!

SantiagoPosted on10:45 pm - Feb 27, 2013

Thank you very much!
Because I was using some proxy code (who does the reading) written by another one, it is a very useful solution for my code.

Søren DalbyPosted on6:57 am - Nov 4, 2010

Just what I needed. Thanks 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.