Start another program and wait until it finishes in C#


The example Open a file with the system’s default application in C# shows how to open a file with the program associated with that type of file. The file opens and the program that started it continues without waiting. Sometimes, however, it’s useful to make the starting program wait until the other program has finished.

This example displays a file in a RichTextBox. You can edit the text, but the program provides no formatting features. Instead it lets you edit the file in WordPad. The following code shows how the program launches WordPad to edit the file and then waits for WordPad to finish.

// Allow the user to edit the file with WordPad.
private void btnEdit_Click(object sender, EventArgs e)
    // Hide.
    this.ShowInTaskbar = false;

    // Save the current text into the file.

    // We will open Filename with wordpad.exe.
    ProcessStartInfo start_info =
        new ProcessStartInfo("wordpad.exe", Filename);
    start_info.WindowStyle = ProcessWindowStyle.Maximized;

    // Open wordpad.
    Process proc = new Process();
    proc.StartInfo = start_info;

    // Wait for wordpad to finish.

    // Reload the file.

    // Unhide.
    this.ShowInTaskbar = true;

While the program waits for WordPad to finish, it’s stuck and can’t do anything. It can’t update its display and cannot respond to the user. To avoid awkwardness, the code first hides the program’s form. It sets ShowInTaskbar to false so the user won’t see the program’s icon in the taskbar or in the Alt-Tab display. It then hides the program so it isn’t visible.

Next the code saves whatever is currently in the RichTextBox, in case the user has made any changes.

The code then builds a ProcessStartInfo object to describe how it will start the new process. The constructor’s parameters give the name of the program to execute (wordpad.exe) and any command line parameters that the program should take. In this case, the single parameter gives the name of the file that WordPad should open. The code also sets the object’s WindowStyle property so WordPad starts maximized.

Next the code creates a Process object, sets its StartInfo property to the ProcessStartInfo object it created, and calls its Start method to start the process. That launches WordPad.

The code then calls the Process‘s WaitForExit method to wait until WordPad exits. The program is stuck on this line of code waiting until WordPad exits.

Next the code reloads the file in case the user made any changes with WordPad. It finishes by restoring the program to the taskbar and redisplaying its form.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in files, programs, system and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Start another program and wait until it finishes in C#

  1. Ello says:

    WaitForExit() makes the current thread wait until the associated process terminates. It should be called after all other methods are called on the process. To avoid blocking the current thread, use the Exited event.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.