Wait a Process For Exit : ProcessStartInfo « Development « C# / CSharp Tutorial





using System;
using System.Diagnostics;

class MainClass
{
    public static void Main()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();

        startInfo.FileName = "notepad.exe";
        startInfo.Arguments = "file.txt";
        startInfo.WorkingDirectory = @"C:\Temp";
        startInfo.WindowStyle = ProcessWindowStyle.Maximized;
        startInfo.ErrorDialog = true;

        Process process;

        try
        {
            process = Process.Start(startInfo);

            Console.WriteLine("Waiting 30 seconds for process to finish.");

            if (process.WaitForExit(30000))
            {
                Console.WriteLine("Process terminated.");
            }
            else
            {
                Console.WriteLine("Timed out waiting for process to end.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Could not start process.");
            Console.WriteLine(ex);
        }
    }
}
Waiting 30 seconds for process to finish.
Process terminated.










14.16.ProcessStartInfo
14.16.1.Create ProcessStartInfo
14.16.2.Start a Process by using ProcessStartInfo
14.16.3.Wait a Process For Exit
14.16.4.Process Window Style: Maximized