C#: Launch a process then Wait for It to Finish.

person Jason Huangfolder_openC#, Codelocal_offer, access_time September 9, 2009

Usually when you launch a program with C# code, it just runs the program and continue on with the rest of the code, it does not care if a program is finished or not.

If for some reason you want C# program to launch a program, then hold the execution until after you close the program. (Until the program is exited.)

For example, the following code will try launch the file: instruction.txt. It will open the text file with the default windows program (which usually is notepad.exe), the code will not continue until instruction.txt is closed.

The trick here is the function to Wait for a program to exit (WaitForExit(), then test to see if the program has exited (HasExited).

string filename = "instruction.txt";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@filename);
System.Diagnostics.Process rfp = new System.Diagnostics.Process();
rfp = System.Diagnostics.Process.Start(psi);
if (rfp.HasExited)
//execute other code after the program has closed
MessageBox.ShowDialog("The program is done.");


  1. Appreciate it. I have been searching over the web to find how to run windows process and waiting for it to finish. Must be my lucky day! 🙂

  2. I hope you don’t mind, I just posted your tip to stackoverflow.com to help out a fellow programmer.

    Here is the link to the question:

  3. I am glad that my little tip can help people out.

    The purpose of my blog is to share my knowledge.

  4. Interesting, I`ll quote it on my site later.

  5. Thanks, that really helped =)

  6. This is not work when you start process like this System.Diagnostics.Process.Start(“mailto:?”);

  7. The first assignement to rfp is useless, because its directly overwritten by the second statement:

    System.Diagnostics.Process rfp = new System.Diagnostics.Process();
    rfp = System.Diagnostics.Process.Start(psi);

  8. Jason Huang

    You’re right. This is meant for beginner tutorial, I deliberately try to show the whole initialization.

  9. Very nice post. I just stumbled upon your blog and wanted
    to say that I have really loved browsing your blog posts.
    After all I will be subscribing on your feed and I am
    hoping you write once more soon!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>