Start another program and wait until it finishes in C#

start

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;
    this.Hide();

    // Save the current text into the file.
    rchText.SaveFile(Filename);

    // 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;
    proc.Start();

    // Wait for wordpad to finish.
    proc.WaitForExit();

    // Reload the file.
    rchText.LoadFile(Filename);

    // Unhide.
    this.ShowInTaskbar = true;
    this.Show();
}

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.

Leave a Reply

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