Click here to Skip to main content
14,772,794 members
Articles » General Programming » Threads, Processes & IPC » Processes
Posted 30 Jul 2003


165 bookmarked

Launching a process and displaying its standard output

Rate me:
Please Sign up or sign in to vote.
4.92/5 (62 votes)
31 Aug 2003CPOL
This article shows how to launch a process (such as a bat file, perl script, console program) and have its standard output displayed on a windows form.

Sample Image - LaunchProcess.png


I wanted to launch a script from a Windows Form application and display the standard output in a text box as the process was running.  Surely you're not surprised to learn that multithreading is involved.  It turns out you'll have at least four threads running to do this simple task.  To keep things simple and sane, I've reused code from other another source, so I must first give credit to the MSDN article "Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads"  by I.D. Griffiths.  I highly suggest reading this article for more background on multithreading in Windows Forms applications.  Thanks also to Chad Christensen for his suggestions in using a RichTextBox.

Creating a class to call a process

A script or executable can be run using System.Diagnostics.Process.  The string FileName is set to the executable (e.g. perl.exe, Run.bat, ConsoleApplication.exe).  The string Arguments is set to the command-line arguments for that executable (e.g., filename1.txt filename2.txt, etc).  The following code will start that executable.

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = FileName;
process.StartInfo.Arguments = Arguments;
process.StartInfo.WorkingDirectory = WorkingDirectory;

Notice that the standard output and standard error have both been redirected.  There are two StreamReaders in the Process class that can be used to read the output: Process.StandardOutput and Process.StandardError.  Often, the output is not read until after the process has finished, as in the following:

string output = process.StandardOutput.ReadToEnd();

Reading to the end will not work for this application, since we want to read the data as the process is running.

Multiple threads

The solution is to use multiple threads.  One thread is dedicated to running the process and two more threads are dedicated to reading the standard error and standard output.  This is mentioned in MSDN documentation.  Each of these two threads will run a simple function that sits in a loop reading from the stream until the stream is closed.

void ReadStdOut()
    string str;
    while ((str = process.StandardOutput.ReadLine()) != null)
        // do something with str

After each line is read into str, we would like to notify a windows form to display the text.  Raising an event is probably the best way to accomplish this.  For every new line of text received (on either StandardOutput or StandardError) an event will be raised.  A windows form class can subscribe to these events and update a text box.  Simple, but it won't quite work without some additional work.

Important rule of windows forms

There is an important rule of windows forms and multithreading.  Controls are (almost entirely) not thread safe.  This means that an event raised from any thread other than the UI Thread cannot use methods or properties of a control.  There are a few methods guaranteed to be safe including Control.Invoke and Control.BeginInvoke.  These methods are used to run a function on the UI thread. 

Thankfully, we can inherit from the class AsyncOperation (written by I.D. Griffiths from the above mentioned MSDN article) to solve several problems.  First, this class allows us to raise an event on a UI thread of a hosting or target control.  The above function becomes:

public delegate void DataReceivedHandler(object sender,
    DataReceivedEventArgs e);

public event DataReceivedHandler StdOutReceived;

void ReadStdOut()
    string str;
    while ((str = process.StandardOutput.ReadLine()) != null)
        FireAsync(StdOutReceived, this, new DataReceivedEventArgs(str));

FireAsync is a method provided by the class AsyncOperation.  It raises an event (or more specifically invokes any delegate) on the UI thread of a form or control.  StdOutReceived is the event that will be raised.  DataReceivedEventArgs is a class derived from EventArgs that has a single string containing the text to be displayed (its definition is not shown here for brevity).

The second thing AsyncOperation provides is a method of canceling a process.  Let's take a look at that class in more detail.

Inheriting from AsyncOperation

AsyncOperation is an abstract base class that assists in creating cancelable worker threads that can fire events back on a UI control (or form).  It provides two main methods that are called by a form class: Start() and Cancel().

AsyncOperation requires that one method be overridden: DoWork(). This method is called when the Start() method is called.  As the method runs, it is expected to watch for a flag CancelRequested that is set from a call to Cancel().  If the flag is true, the method should acknowledge the cancel and return.

The implementation of DoWork() is as follows:

void protected override void DoWork()()
    // Start a new process for the cmd
    Process process = new Process();
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.FileName = FileName;
    process.StartInfo.Arguments = Arguments;
    process.StartInfo.WorkingDirectory = WorkingDirectory;

    // Invoke stdOut and stdErr readers - each
    // has its own thread to guarantee that they aren't
    // blocked by, or cause a block to, the actual
    // process running (or the gui).
    new MethodInvoker(ReadStdOut).BeginInvoke(null, null);
    new MethodInvoker(ReadStdErr).BeginInvoke(null, null);

    // Wait for the process to end, or cancel it
    while (! process.HasExited)
        Thread.Sleep(SleepTime); // sleep
        if (CancelRequested)
            // Not a very nice way to end a process,
            // but effective.


The methods DoWork()ReadStdOut(), and ReadStdErr(), the properties FileName and Arguments, and the events StdOutReceived and StdErrReceived are all added to a class ProcessCaller which derives from AsyncOperation.  Both classes can be downloaded as part of the zipfile at the top of the page.

The form

As shown in the picture above, the form is quite simple.  It consists of a rich text box to show the standard output and standard input, a button to run a process (Ok), and a button to cancel the process (Cancel).

The Ok button calls the Start() method on ProcessCaller and the Cancel button calls the Cancel() method.  The events StdOutRecieved and StdErrReceived are handled by the following function:

private void writeStreamInfo(object sender, DataReceivedEventArgs e)
    this.richTextBox1.AppendText(e.Text + Environment.NewLine);


Adding a progress bar to the form is one nice improvement for the user interface.  Of course, you have to know the progress of the program being run.  One option is to have the script tell you explicitly in the standard output with lines such as: "Percent completion = 30".  Your "writeStreamInfo" function would filter those lines and update a progress bar.

Standard Error can be displayed in red or some other color (or in a separate rich text box) to highlight any errors found.  At the end of the process, a dialog could be displayed with a list of all errors.

Providing support for standard input shouldn't be too difficult, but integrating it with the windows form may be tough.  Perhaps a separate single-line text box whose contents are sent to the standard input stream through a method on ProcessCaller.

These are just a few ideas of improvements you can make.


Using the class AsyncOperation reduced the design complexity of this program.  Hopefully, the class ProcessCaller will provide you just as much help in reducing the complexity of running scripts and monitoring the output.

Revision History

  • 2003-Jul-31 : Original Post
  • 2003-Aug-05 : Fixed some spelling / grammatical mistakes (oops)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mike Mayer
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
abberdeen22-Jul-18 13:28
Memberabberdeen22-Jul-18 13:28 
GeneralMy vote of 5 Pin
abberdeen22-Jul-18 13:27
Memberabberdeen22-Jul-18 13:27 
QuestionHow about discovering a secondary process Pin
mwpowellhtx26-Apr-18 21:32
Membermwpowellhtx26-Apr-18 21:32 
QuestionStartProcess from Class not Form Pin
Eid Shkhaidem15-Nov-17 15:51
MemberEid Shkhaidem15-Nov-17 15:51 
PraiseThis Is Awesome 2017 and still good Pin
actualmanx4-Nov-17 15:07
Memberactualmanx4-Nov-17 15:07 
QuestionLaunching a process and displaying its standard output Pin
Member 989245626-May-17 14:10
MemberMember 989245626-May-17 14:10 
GeneralMy vote of 5 Pin
Dave Coates14-Dec-16 2:30
MemberDave Coates14-Dec-16 2:30 
QuestionRunning Multiple process Pin
vinvinodk25-Jun-15 4:21
Membervinvinodk25-Jun-15 4:21 
GeneralMy vote of 5 Pin
actualmanx19-Feb-14 5:31
Memberactualmanx19-Feb-14 5:31 
QuestionFails to catch exceptions from start process Pin
grafne7919-Aug-11 1:25
Membergrafne7919-Aug-11 1:25 
GeneralDisconnectedContext was detected Errror Pin
Jason_H28-Aug-09 16:48
MemberJason_H28-Aug-09 16:48 
GeneralWant to do same in Web forms Pin
t_bala1822-Jun-09 16:48
Membert_bala1822-Jun-09 16:48 
GeneralCaptureConsole.DLL - A Universal Console Output Redirector for all Compilers Pin
Elmue18-Jun-09 14:57
MemberElmue18-Jun-09 14:57 
QuestionSuspending and resuming the process Pin
ohad-oz21-Nov-08 7:59
Memberohad-oz21-Nov-08 7:59 
QuestionUsing "redirectstandardinput" with this Pin
immortaldeath19-Sep-08 9:47
Memberimmortaldeath19-Sep-08 9:47 
GeneralA bit of a newb here, but figured I would give it a try. Pin
immortaldeath3-Sep-08 19:58
Memberimmortaldeath3-Sep-08 19:58 
GeneralAbout the article "Launching a process and displaying the standard output" on Code project Pin
Anupama Sankaran28-Aug-08 9:58
MemberAnupama Sankaran28-Aug-08 9:58 
QuestionTerminate process on standard error Pin
oidele27-Oct-07 11:19
Memberoidele27-Oct-07 11:19 
QuestionGUI blocked Pin
MeJVZ6-Jul-07 9:17
MemberMeJVZ6-Jul-07 9:17 
GeneralYour code does not work always VERY STRANGE Pin
Elmue14-Dec-06 23:08
MemberElmue14-Dec-06 23:08 

First thanks for the great article.
With your demo application which writes to the console (SmallConsoleProgram.exe) all works fine.

But what I want to do is display the output from Mplex.
Mplex is a multiplexer which multiplexes a MPEG file from separate video and audio files.
Mplex writes all its output to the console.
Immediately after Mplex has started it writes the first line of output to the DOS box, which is always this:

INFO: [???] mplex version 2.2.2 ($Date: 2003/05/13 20:27:15 $)

The strange thing is that your progressform will display NOTHING while Mplex is running (which may take 10 minutes or more for big video files).
The textbox stays empty all the time.
Not before the Mplex process exited ALL the output appears at once!

But when I start Mplex in the DOS box (Cmd.exe) I see that it immediately starts to write its output!

How is it possible that your demo works with one console application but not with another one ???

Has anyone an explanation ?

If found another project which does the same but written in C++.
It also displays the output AFTER Mplex has finished!


This is what I changed to your project:

processCaller.FileName  = "C:\\Program Files\\Muxer\\mplex.exe";
processCaller.Arguments = "-f 8 -o \"E:\\Videos\\MyVideo.mpg\" \"E:\\Videos\\MyVideo.m2v\" \"E:\\Videos\\MyVideo.mp2";

When the mplex process has finished this appears ALL at once in the GUI:

   INFO: [???] mplex version 2.2.2 ($Date: 2003/05/13 20:27:15 $)
   INFO: [???] File E:\Videos\MyVideo.m2v looks like an MPEG Video stream.
   INFO: [???] File E:\Videos\MyVideo.mp2 looks like an MPEG Audio stream.
   INFO: [???] Video stream 0: profile 8 selected - ignoring non-standard options!
   INFO: [???] Found 1 audio streams and 1 video streams
   INFO: [???] Selecting dvdauthor DVD output profile
   INFO: [???] Multiplexing video program stream!
   INFO: [???] Scanning for header info: Video stream e0 (E:\Videos\MyVideo.m2v) 
   INFO: [???] VIDEO STREAM: e0
   INFO: [???] Frame width     : 720
   INFO: [???] Frame height    : 576
   INFO: [???] Aspect ratio    : 4:3 display
   INFO: [???] Picture rate    : 25.000 frames/sec
   INFO: [???] Bit rate        : 15000000 bits/sec
   INFO: [???] Vbv buffer size : 229376 bytes
   INFO: [???] CSPF            : 0
   INFO: [???] Scanning for header info: Audio stream c0 (E:\Videos\MyVideo.mp2)
   INFO: [???] Audio version  : 1.0
   INFO: [???] Layer          :        2
   INFO: [???] CRC checksums  :       no
   INFO: [???] Bit rate       :    16384 bytes/sec (128 kbit/sec)
   INFO: [???] Frequency      :     48000 Hz
   INFO: [???] Mode           :        1 joint stereo
   INFO: [???] Mode extension :        0
   INFO: [???] Copyright bit  :        1 copyright protected
   INFO: [???] Original/Copy  :        1 original
   INFO: [???] Emphasis       :        0 none
   INFO: [???] SYSTEMS/PROGRAM stream:
   INFO: [???] rough-guess multiplexed stream data rate    : 15359248
   INFO: [???] target data-rate specified               : 10080000
++ WARN: [???] Target data rate lower than computed requirement!
++ WARN: [???] N.b. a 20% or so discrepancy in variable bit-rate
++ WARN: [???] streams is common and harmless provided no time-outs will occur
   INFO: [???] Run-in Sectors = 89 Video delay = 13019 Audio delay = 0
   INFO: [???] New sequence commences...
   INFO: [???] Video e0: buf= 237568 frame=000000 sector=00000000
   INFO: [???] Audio c0: buf=   4096 frame=000000 sector=00000000
++ WARN: [???] Discarding incomplete final frame MPEG audio stream c0!
   INFO: [???] Scanned to end AU 2280
   INFO: [???] STREAM e0 completed @ frame 2280.
   INFO: [???] STREAM c0 completed @ frame 3800.
   INFO: [???] Multiplex completion at SCR=8207506.
   INFO: [???] Video e0: buf= 109094 frame=002280 sector=00022205
   INFO: [???] Audio c0: buf=   1024 frame=003800 sector=00000723
   INFO: [???] Video Stream length:    44771532 bytes
   INFO: [???] Sequence headers:      191
   INFO: [???] Sequence ends   :        1
   INFO: [???] No. Pictures    :     2280
   INFO: [???] No. Groups      :      191
   INFO: [???] No. I Frames    :      191 avg. size 58006 bytes
   INFO: [???] No. P Frames    :      570 avg. size 23285 bytes
   INFO: [???] No. B Frames    :     1520 avg. size 13434 bytes
   INFO: [???] Average bit-rate :  3925600 bits/sec
   INFO: [???] Peak bit-rate    :  5729600  bits/sec
   INFO: [???] BUFFERING min 15 Buf max 134306
   INFO: [???] Audio stream length 1459588 bytes.
   INFO: [???] Syncwords      :     3802
   INFO: [???] Frames         :     3802 padded
   INFO: [???] Frames         :        0 unpadded
   INFO: [???] BUFFERING min 15 Buf max 395
   INFO: [???] MUX STATUS: no under-runs detected.

GeneralRe: I found the reason Pin
Elmue15-Dec-06 5:32
MemberElmue15-Dec-06 5:32 
GeneralRe: I found the reason Pin
RYU^^1-Mar-07 19:10
MemberRYU^^1-Mar-07 19:10 
GeneralRe: I found the reason Pin
cggiriraj17-Mar-08 5:00
Membercggiriraj17-Mar-08 5:00 
GeneralRe: I found the reason Pin
zeiksz18-Jun-09 1:34
Memberzeiksz18-Jun-09 1:34 
QuestionVS2005 update? Pin
taumuon25-Jul-06 8:11
Membertaumuon25-Jul-06 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.