-
October 15th, 2010, 09:18 PM
#1
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.
-
October 15th, 2010, 09:39 PM
#2
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?
-
October 16th, 2010, 11:05 AM
#3
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.
-
October 16th, 2010, 01:09 PM
#4
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.
-
October 16th, 2010, 03:06 PM
#5
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
-
October 16th, 2010, 04:29 PM
#6
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.
-
October 16th, 2010, 04:31 PM
#7
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.
-
October 16th, 2010, 05:09 PM
#8
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
-
October 16th, 2010, 06:02 PM
#9
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
-
October 16th, 2010, 07:33 PM
#10
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.
-
October 16th, 2010, 08:45 PM
#11
Re: Running a console application in a windows form
Originally Posted by mikeroq
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?
-
October 16th, 2010, 11:42 PM
#12
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.
-
October 17th, 2010, 12:22 AM
#13
Re: Running a console application in a windows form
Originally Posted by mikeroq
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.
-
October 17th, 2010, 09:06 AM
#14
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)
{
}
}
}
-
January 18th, 2018, 04:01 PM
#15
Re: Running a console application in a windows form
Originally Posted by BigEd781
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|