Posts Tagged ‘.Net’

ProgressStream: A Stream with Read and Write events

January 19th, 2010

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", 

WPF: Center Child Window

May 7th, 2009

Here’s a method I’ve found for centering a window to either its parent or the main window for the application, in WPF. It’s not too far different from how you do it in WinForms.

For the child window set it’s WindowStartupLocation to “CenterOwner.” This will cause it to show in the center of the owning Window.

<Window x:Class="WpfApplication1.TestChild"
    Title="TestChild" Height="300" Width="300"

Now all that’s left to do is set its owner before displaying it. If the code you’re using to display the window is running inside of a Window class, then you can just use this.

TestChild testWindow = new TestChild();
testWindow.Owner = this;

This isn’t always the case however, sometimes you need to display the child window from code running on a page or a user control. In this case you want the child window to be centered to the main window of the application.

TestChild testWindow = new TestChild();
testWindow.Owner = Application.Current.MainWindow;