none
How can you attach a console to a process? RRS feed

  • Question

  • I think I'm asking the right question for what I need.  I also hope this is in the right topic...

    I have a windows application that was originally a console application.  It uses a class to run a number of methods.  Each method prints various messages regarding status using Console.Writeline.  This worked when it was a console application, but I've changed it to a windows application and still need to display the messages somewhere.

    Is there a way to open a console in my windows application so that all the Console.Writelines will appear in it?  Or is there a better way to do this?

    Thanks in advance!
    • Changed type im1dermike2 Wednesday, February 25, 2009 3:51 PM
    Wednesday, February 25, 2009 3:41 PM

Answers

  • This code create us TextWriter as a Base Class.
    You are able override a number of the Text Functions and send it to a RichText view. The derived object is then istantiated in a form that displays the richTextControl.

    The key to this function is Console.SetOut(rtw);
    This allows you to cal Console.WriteLine from anyware in your program and the text will end be directed to your istantiated class RichTextWriter. RichTextWriter then writes the text to a rich text control displayed on OutputView From.

        public partial class OutputView : Form
        {
            public RichTextWriter rtw = new RichTextWriter();

            public OutputView()
            {
                InitializeComponent();

                rtw.richText.Parent = this;
                rtw.richText.Location = new Point(0, 0);
                rtw.richText.Size = new Size(Width, Height);

                Console.SetOut(rtw);
            }

            private void OutputView_Resize(object sender, EventArgs e)
            {
                rtw.richText.Size = new Size(Width - 8, Height- 33);
            }
        }

        public class RichTextWriter : TextWriter
        {
            public RichTextBox richText;

            public RichTextWriter()
            {
                richText = new RichTextBox();
                richText.ReadOnly = true;
                richText.BackColor = Color.White;
            }

            public override Encoding Encoding
            {
                get { return Encoding.Default; }
            }

            public override void Write(string value)
            {
                richText.AppendText(value);
                richText.Refresh();
            }

            public override void WriteLine()
            {
                richText.AppendText("\n");
                richText.Refresh();
            }

            public override void WriteLine(string value)
            {
                richText.AppendText(value + "\n");
                richText.Refresh();
            }
        }

    Good Luck
    Nick

    • Marked as answer by im1dermike2 Thursday, February 26, 2009 2:51 PM
    Thursday, February 26, 2009 12:12 AM

All replies

  • Does anyone read these forums?
    Wednesday, February 25, 2009 7:43 PM
  • Just change it back to a Console mode app, you'll get both.  A far more common approach would be to use logging with Trace or log4net.
    Hans Passant.
    • Proposed as answer by nickw89509 Thursday, February 26, 2009 12:12 AM
    Wednesday, February 25, 2009 11:10 PM
    Moderator
  • This code create us TextWriter as a Base Class.
    You are able override a number of the Text Functions and send it to a RichText view. The derived object is then istantiated in a form that displays the richTextControl.

    The key to this function is Console.SetOut(rtw);
    This allows you to cal Console.WriteLine from anyware in your program and the text will end be directed to your istantiated class RichTextWriter. RichTextWriter then writes the text to a rich text control displayed on OutputView From.

        public partial class OutputView : Form
        {
            public RichTextWriter rtw = new RichTextWriter();

            public OutputView()
            {
                InitializeComponent();

                rtw.richText.Parent = this;
                rtw.richText.Location = new Point(0, 0);
                rtw.richText.Size = new Size(Width, Height);

                Console.SetOut(rtw);
            }

            private void OutputView_Resize(object sender, EventArgs e)
            {
                rtw.richText.Size = new Size(Width - 8, Height- 33);
            }
        }

        public class RichTextWriter : TextWriter
        {
            public RichTextBox richText;

            public RichTextWriter()
            {
                richText = new RichTextBox();
                richText.ReadOnly = true;
                richText.BackColor = Color.White;
            }

            public override Encoding Encoding
            {
                get { return Encoding.Default; }
            }

            public override void Write(string value)
            {
                richText.AppendText(value);
                richText.Refresh();
            }

            public override void WriteLine()
            {
                richText.AppendText("\n");
                richText.Refresh();
            }

            public override void WriteLine(string value)
            {
                richText.AppendText(value + "\n");
                richText.Refresh();
            }
        }

    Good Luck
    Nick

    • Marked as answer by im1dermike2 Thursday, February 26, 2009 2:51 PM
    Thursday, February 26, 2009 12:12 AM
  • Nick,

    This is EXACTLY what I needed.  Thanks so much!
    Thursday, February 26, 2009 2:51 PM
  • I've implemented your solution, Nick.  Is there some sort of buffer issue?  Sometimes it won't print a couple WriteLines and then it will print like 3 all at once.  My use of this revolves around knowing what part of the process the program is at so it's pretty important that I know where it is, making this a big issue.

    Also, the functions my program completes are pretty memory-intensive.  Since I implemented the richTextWriter, it seems to freeze the application window.  It exhibits the signs of a "Not Responding" program, but it's not actually frozen.
    Thursday, February 26, 2009 3:20 PM
  • I have used this code to do in application debuging. I have never seen the issues you are speaking of with the RichTextWriter. I have seen it on a simular issue when I redirected StdOut of a process that I was spawning from my program. Never really got it to work 100%. I do believe that it is a buffering issue but the textwriter does provide control over many of these parameters. I would suggest you explore what inherting TextWriter gives you.

    Are you sending a large amount of text to the window??

    Nick
    Thursday, February 26, 2009 4:05 PM
  • Nope.  Just single lines eg. "Backing up databases..."  Do you think adding a Thread.Sleep(2000) before and/or after each WriteLine would do anything?

    Also, the code didn't seem to scroll down vertically when the textbox filled up so you couldn't see the newest lines.  I created this event handler to do that which works:

            rtw.richText.TextChanged += new EventHandler(richText_TextChanged);
            public void richText_TextChanged(object sender, EventArgs args)
            {
                rtw.richText.SelectionLength = 0;
                rtw.richText.SelectionStart = rtw.richText.Text.Length;
                rtw.richText.ScrollToCaret();
            }
    Thursday, February 26, 2009 5:19 PM
  • Thinking  about this I suspect that you have a long task and are attempting to monitor the progress of the task. I think the problems is that the updates are not getting to the window

    If I am correct this should fix it:
    Console.Write*****
    System.Windows.Forms.Application.DoEvents();

    This should force the pending events to update the window.

    Also:
    You may want to verify the problem by adding
    System.Diagnostics.Debug.Writeline () to your TextWriter implementation.
    Now make sure you can see the debug window and your application at the same time.

    1 of 2 things are going to happen
    1) The messages will appear at the same time in your debug window and the app {ALL THE TIME}

    2) Occasionally there will be a noticable delay between the message appearing in the debug and the message appearing in the App window.

    1 is bad
    2 is good

    1) Implies a buffering problem and again you will need to find a solution by changing the TextWrite 2
    2) Implies a update problem cause by db Update. If this is the problem the DoEvents() should be the correct answere.

    Good Luck
    Nick
    • Proposed as answer by nickw89509 Thursday, February 26, 2009 6:03 PM
    Thursday, February 26, 2009 6:03 PM