CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 2010
    Posts
    7

    Running a console application in a windows form

    I am making a c# windows form application (.net 4.0) and I am looking to run a console application inside my GUI application. I want to be able to interact with the program (view the output, provide input if necessary) as if it was running in the command prompt.

    Is this possible at all? At the least starting the console application hidden, and viewing the output from the GUI application?

    I hope this makes sense, I don't know much C#, some C++, I haven't written a program in about 6 years >.> I'm kinda out of date.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Running a console application in a windows form

    What are you hoping to accomplish by doing this? What do you hope to gain by adding a windows app around a console window?

  3. #3
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    I have multiple console programs that I want to run inside one GUI that can be minimized to the tray, so the three console windows are not on the screen. I need to see the output of the programs to monitor them while they are running and pass commands to them. I have found little information on this, I've gotten to the point where I can view the output after the program finishes but these programs run continually.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Running a console application in a windows form

    See if you can PIPE the output to a file. It'd be easier to use a FileWatcher to monitor new files, to display the data.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Running a console application in a windows form

    To add to dglienna's (great) suggestion, this is how you pipe data between programs, to a text file, etc.

    Code:
    C:\>SomeProgramThatHasOutput.exe >> myFile.txt
    ...
    C:\>SomeProgramThatHasOutput.exe >> AProgramThatTakesInput.exe

  6. #6
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    Dumping the program results to a file only occurs on exit of the program correct? If so, this will not work and I need to monitor the program while it runs, realtime output and providing input.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Running a console application in a windows form

    No, it redirects standard out to whatever is on the rhs of the >> operator. This is how programs communicate with one another.

  8. #8
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    I tested this method and it doesn't dump any data into the text file until the program terminates

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Running a console application in a windows form

    Attempting to run several programs in console windows controlled by a form app isn't possible afaik.

    Instead you can run console applications in what appears to be console windows by redirecting std input/output. Just create the process, redirect std input/output to a window that has the appearance (ie. styling) of a console window.

    See the following cg thread:

    http://www.codeguru.com/forum/showth...hreadid=460918

  10. #10
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    This is what I'm trying to do,


    I want realtime input and output. I've tried various code and searched all day and haven't found anything in C# that will work for me. I couldn't get the OutputDataRecieved to work.
    Last edited by mikeroq; October 16th, 2010 at 07:36 PM.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Running a console application in a windows form

    Quote Originally Posted by mikeroq View Post
    This is what I'm trying to do,


    I want realtime input and output. I've tried various code and searched all day and haven't found anything in C# that will work for me. I couldn't get the OutputDataRecieved to work.
    Did you try the CGStdOutput.zip sample attached in the other post?

    Do you have the code that you tried?

  12. #12
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    I tried that, it seems to be passing something back through to the console application cause my console application says its an invalid command, it just loops that and crashes the windows form.
    Last edited by mikeroq; October 16th, 2010 at 11:46 PM.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Running a console application in a windows form

    Quote Originally Posted by mikeroq View Post
    I tried that, it seems to be passing something back through to the console application cause my console application says its an invalid command, it just loops that and crashes the windows form.
    If you want help, you're going to need to post your code.

  14. #14
    Join Date
    Oct 2010
    Posts
    7

    Re: Running a console application in a windows form

    The code is the same as whats in the zip file, except for it opens one of my console applications.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    
    namespace CGStdOutput
    {
        public partial class Form1 : Form
        {
            public Form1( )
            {
                InitializeComponent( );
            }
    
            private void _btnRun_Click( object sender, EventArgs e )
            {
                _btnRun.Enabled = false;
    
                if( null != _process )
                {
                    _process.Dispose( );
                }
    
                _process = new Process( );
                _process.StartInfo.FileName = "arcemu-logonserver.exe";
    
                _process.StartInfo.UseShellExecute = false;
    
                _process.EnableRaisingEvents = true;
                _process.StartInfo.CreateNoWindow = true;
                _process.StartInfo.RedirectStandardOutput = true;
    
                _process.OutputDataReceived += new DataReceivedEventHandler( OnOutputDataReceived );
                _process.Exited += new EventHandler( OnProcessExited );
    
                _process.Start( );
    
                _process.BeginOutputReadLine( );
            }
    
            /// <summary>
            /// Standard Output event handler - called when standard output text is available from
            /// the launched process.
            /// This event handler gets called from a different thread than the main UI thread.
            /// As such we need to use a delegate to access the UI thread.
            /// </summary>
            void OnOutputDataReceived( object sender, DataReceivedEventArgs e )
            {
                if( _listBox.InvokeRequired && !String.IsNullOrEmpty( e.Data ) )
                {
                    // We use an anonymous delegate here
                    _listBox.Invoke( new MethodInvoker( delegate( ) { _listBox.Items.Add( e.Data ); } ) );
                }
            }
    
            /// <summary>
            /// Called when process exits
            /// </summary>
            void OnProcessExited( object sender, EventArgs e )
            {
                if( _btnRun.InvokeRequired )
                {
                    // We use an anonymous delegate here
                    _btnRun.Invoke( new MethodInvoker( delegate( ) { _btnRun.Enabled = true; } ) );
                }
            }
    
            private Process _process = null;
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

  15. #15
    Join Date
    Jan 2018
    Posts
    1

    Re: Running a console application in a windows form

    Quote Originally Posted by BigEd781 View Post
    To add to dglienna's (great) suggestion, this is how you pipe data between programs, to a text file, etc.

    Code:
    C:\>SomeProgramThatHasOutput.exe >> myFile.txt
    ...
    C:\>SomeProgramThatHasOutput.exe >> AProgramThatTakesInput.exe

    ...Uh-oh, you just destroyed AProgramThatTakesInput.exe by overwriting it with text.

    Perhaps you meant to say
    Code:
    C:\>SomeProgramThatHasOutput.exe | AProgramThatTakesInput.exe

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured