Process.WaitForExit : Process « System.Diagnostics « C# / C Sharp by API






Process.WaitForExit

 

using System;
using System.Threading;
using System.Diagnostics;

class MainClass
{
    public static void Main()
    {
        using (Process process = Process.Start("notepad.exe", @"c:\SomeFile.txt"))
        {
            Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
            Thread.Sleep(5000);
            Console.WriteLine("Terminating Notepad with CloseMainWindow.");
            if (!process.CloseMainWindow())
            {
                Console.WriteLine("CloseMainWindow returned false - " + " terminating Notepad with Kill.");
                process.Kill();
            }
            else
            {
                if (!process.WaitForExit(2000))
                {
                    Console.WriteLine("CloseMainWindow failed to" + " terminate - terminating Notepad with Kill.");
                    process.Kill();
                }
            }
        }
    }
}

   
  








Related examples in the same category

1.Process.CloseMainWindow
2.Process.Exited
3.Process.GetCurrentProcess()
4.Process.GetProcessById
5.Process.Kill()
6.Process.Modules
7.Process.PeakWorkingSet64
8.Process.PriorityClass
9.Process.PrivateMemorySize
10.Process.ProcessName
11.Process.StandardOutput.ReadToEnd()
12.Process.Start
13.Process.StartInfo.RedirectStandardOutput
14.Process.Threads
15.Process.TotalProcessorTime
16.Process.VirtualMemorySize
17.Process.WorkingSet