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






Process.CloseMainWindow

 

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.Exited
2.Process.GetCurrentProcess()
3.Process.GetProcessById
4.Process.Kill()
5.Process.Modules
6.Process.PeakWorkingSet64
7.Process.PriorityClass
8.Process.PrivateMemorySize
9.Process.ProcessName
10.Process.StandardOutput.ReadToEnd()
11.Process.Start
12.Process.StartInfo.RedirectStandardOutput
13.Process.Threads
14.Process.TotalProcessorTime
15.Process.VirtualMemorySize
16.Process.WaitForExit
17.Process.WorkingSet