public ConsoleOutputController(RunModelSystemPage page)
            {
                var previousConsole = Console.Out;

                writer        = new StreamWriter(memoryStream, System.Text.Encoding.Unicode);
                page.OldCaret = 0;
                Console.SetOut(this.writer);
                new Task(() =>
                {
                    try
                    {
                        var lastPosition = 0L;
                        if (lastPosition == 0)
                        {
                            this.Show = Visibility.Hidden;
                            var e     = PropertyChanged;
                            if (e != null)
                            {
                                e(this, new PropertyChangedEventArgs("Show"));
                            }
                        }
                        StreamReader reader = new StreamReader(memoryStream, System.Text.Encoding.Unicode);
                        while (true)
                        {
                            Thread.Sleep(60);
                            writer.Flush();
                            var currentPosition = writer.BaseStream.Position;
                            if (currentPosition > lastPosition)
                            {
                                var buff = new char[(currentPosition - lastPosition) / sizeof(char)];
                                memoryStream.Position = lastPosition;
                                int length            = reader.ReadBlock(buff, 0, buff.Length);
                                if (lastPosition == 0)
                                {
                                    this.Show = Visibility.Visible;
                                    var e     = PropertyChanged;
                                    if (e != null)
                                    {
                                        e(this, new PropertyChangedEventArgs("Show"));
                                    }
                                }
                                lastPosition = currentPosition;
                                if (length > 0)
                                {
                                    page.Dispatcher.Invoke(new Action(() =>
                                    {
                                        page.OldCaret = page.ConsoleOutput.CaretIndex;
                                    }));
                                    this.ConsoleOutput = this.ConsoleOutput + new string( buff, 0, length );
                                    var e = PropertyChanged;
                                    if (e != null)
                                    {
                                        e(this, new PropertyChangedEventArgs("ConsoleOutput"));
                                    }
                                }
                            }
                            if (this.Done)
                            {
                                writer.Dispose();
                                writer = null;
                                return;
                            }
                        }
                    }
                    catch
                    {
                        Console.SetOut(previousConsole);
                    }
                }).Start();
            }
 public ConsoleOutputController(RunModelSystemPage page)
 {
     var previousConsole = Console.Out;
     writer = new StreamWriter( memoryStream, System.Text.Encoding.Unicode );
     page.OldCaret = 0;
     Console.SetOut( this.writer );
     new Task( () =>
         {
             try
             {
                 var lastPosition = 0L;
                 if ( lastPosition == 0 )
                 {
                     this.Show = Visibility.Hidden;
                     var e = PropertyChanged;
                     if ( e != null )
                     {
                         e( this, new PropertyChangedEventArgs( "Show" ) );
                     }
                 }
                 StreamReader reader = new StreamReader( memoryStream, System.Text.Encoding.Unicode );
                 while ( true )
                 {
                     Thread.Sleep( 60 );
                     writer.Flush();
                     var currentPosition = writer.BaseStream.Position;
                     if ( currentPosition > lastPosition )
                     {
                         var buff = new char[( currentPosition - lastPosition ) / sizeof( char )];
                         memoryStream.Position = lastPosition;
                         int length = reader.ReadBlock( buff, 0, buff.Length );
                         if ( lastPosition == 0 )
                         {
                             this.Show = Visibility.Visible;
                             var e = PropertyChanged;
                             if ( e != null )
                             {
                                 e( this, new PropertyChangedEventArgs( "Show" ) );
                             }
                         }
                         lastPosition = currentPosition;
                         if ( length > 0 )
                         {
                             page.Dispatcher.Invoke( new Action( () =>
                                 {
                                     page.OldCaret = page.ConsoleOutput.CaretIndex;
                                 } ) );
                             this.ConsoleOutput = this.ConsoleOutput + new string( buff, 0, length );
                             var e = PropertyChanged;
                             if ( e != null )
                             {
                                 e( this, new PropertyChangedEventArgs( "ConsoleOutput" ) );
                             }
                         }
                     }
                     if ( this.Done )
                     {
                         writer.Dispose();
                         writer = null;
                         return;
                     }
                 }
             }
             catch
             {
                 Console.SetOut( previousConsole );
             }
         } ).Start();
 }