Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1088655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:06:06+00:00 2026-05-16T23:06:06+00:00

I have written a method that accepts a TextWriter as an argument (Usually Console.Out,

  • 0

I have written a method that accepts a TextWriter as an argument (Usually Console.Out, but not necessarily).

When I call this method, some progress info is written to the TextWriter.
However, since this method can run a long time, I want to update my UI with some status information.

Currently, I am using a StringWriter but it does not have any events. So the first time I get a result from the StringWriter is after the method completed.

Now I am searching for a class that inherits from TextWriter and fires an TextChanged event or something like that.
I know that this shouldn’t be to hard to implement, but I bet the CLR Team already did it for me, I just can’t find the appropriate class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:06:06+00:00Added an answer on May 16, 2026 at 11:06 pm

    If anyone is interested, this is a class that extends the StringWriter() class to fire events after every call to writer.Flush().

    I also added the posibillity to automatically call Flush() after every write, since, in my case, a third party component, that did write to the console, didn’t do a flush.

    Sample usage:

    void DoIt()
    {
        var writer = new StringWriterExt(true); // true = AutoFlush
        writer.Flushed += new StringWriterExt.FlushedEventHandler(writer_Flushed);
    
        TextWriter stdout = Console.Out;
        try
        {
            Console.SetOut(writer);
            CallLongRunningMethodThatDumpsInfoOnConsole();
        }
        finally
        {
            Console.SetOut(stdout);
        }
    }
    

    Now I can display some status information just in time and don’t need to wait for the method to finish.

    void writer_Flushed(object sender, EventArgs args)
    {
        UpdateUi(sender.ToString());
    }
    

    And here is the class:

    public class StringWriterExt : StringWriter
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public delegate void FlushedEventHandler(object sender, EventArgs args);
        public event FlushedEventHandler Flushed;
        public virtual bool AutoFlush { get; set; }
    
        public StringWriterExt()
            : base() { }
    
        public StringWriterExt(bool autoFlush)
            : base() { this.AutoFlush = autoFlush; }
    
        protected void OnFlush()
        {
            var eh = Flushed;
            if (eh != null)
                eh(this, EventArgs.Empty);
        }
    
        public override void Flush()
        {
            base.Flush();
            OnFlush();
        }
    
        public override void Write(char value)
        {
            base.Write(value);
            if (AutoFlush) Flush();
        }
    
        public override void Write(string value)
        {
            base.Write(value);
            if (AutoFlush) Flush();
        }
    
        public override void Write(char[] buffer, int index, int count)
        {
            base.Write(buffer, index, count);
            if (AutoFlush) Flush();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this console type thing that accepts a line of C# code, wraps
i have written a method in java like this public boolean ADD(String ID,String Name,String
I have written this clone method for when the parent of the Employee class
I have a method written in VB.NET. It looks like this: Shared Sub SomeMethod(ByVal
I have written a login form, in this I have used, form and method
I have a web method that can accept a XElement argument and I want
I'm refactoring some code and I have written a method that modifies a Dictionary
I have created a method that accepts an object then attempts to serialize the
I have a class (let's call it MyService ) that accepts two dependencies in
I have written an O/R database wrapper that generates some wrapper methods for stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.