1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
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 Process _process = null;
private void _btnRun_Click( object sender, EventArgs e )
{
_btnRun.Enabled = false;
if( null != _process ) _process.Dispose( );
_process = new Process( );
_process.StartInfo.FileName = "Le_nom_du_proframe_console.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( );
}
/// Standard Output event handler - called when standard output text is
/// available fromthe launched process.
/// This event handler gets called from a different thread than the main
/// UIthread. As such we need to use a delegate to access the UI thread.
void OnOutputDataReceived( object sender, DataReceivedEventArgs e )
{ // We use an anonymous delegate here
if( _listBox.InvokeRequired && !String.IsNullOrEmpty( e.Data ) )
_listBox.Invoke( new MethodInvoker(
delegate( ) { _listBox.Items.Add( e.Data ); } ) );
}
/// Called when process exits
void OnProcessExited( object sender, EventArgs e )
{ // We use an anonymous delegate here
if( _btnRun.InvokeRequired )
_btnRun.Invoke( new MethodInvoker(
delegate( ) { _btnRun.Enabled = true; } ) );
}
}
} |
Partager