Terminating Notepad with Kill : Windows Application Process « Windows « C# / CSharp Tutorial






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

class MainClass
{
    public static void Main()
    {
        using (Process process = Process.Start("notepad.exe",@"c:\test.txt"))
        {
            Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
            Thread.Sleep(5000);

            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();
                }
            }
        }
    }
}








29.8.Windows Application Process
29.8.1.Start Notepad as the specified user
29.8.2.Terminating Notepad with CloseMainWindow
29.8.3.Terminating Notepad with Kill
29.8.4.IE processes