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

Thread: Running a console application in a windows form

Hybrid View

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

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

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: Running a console application in a windows form

    Quote Originally Posted by weasel5i2 View Post
    ...Uh-oh, you just destroyed AProgramThatTakesInput.exe by overwriting it with text.

    Perhaps you meant to say
    Code:
    C:\>SomeProgramThatHasOutput.exe | AProgramThatTakesInput.exe
    Well spotted!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  9. #9
    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.

  10. #10
    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

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

    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

  12. #12
    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.

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

    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?

  14. #14
    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.

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

    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.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)