ערוך

שתף באמצעות


Process.CloseMainWindow Method

Definition

Closes a process that has a user interface by sending a close message to its main window.

public:
 bool CloseMainWindow();
public bool CloseMainWindow ();
member this.CloseMainWindow : unit -> bool
Public Function CloseMainWindow () As Boolean

Returns

true if the close message was successfully sent; false if the associated process does not have a main window or if the main window is disabled (for example if a modal dialog is being shown).

Exceptions

The process has already exited.

-or-

No process is associated with this Process object.

Examples

The following example starts an instance of Notepad. It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. The example detects whether the process exits before 10 seconds have elapsed. The example closes the process if it is still running after 10 seconds.

#using <System.dll>

using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int main()
{
   try
   {
      Process^ myProcess;
      myProcess = Process::Start(  "Notepad.exe" );
      
      // Display physical memory usage 5 times at intervals of 2 seconds.
      for ( int i = 0; i < 5; i++ )
      {
         if (  !myProcess->HasExited )
         {
            
            // Discard cached information about the process.
            myProcess->Refresh();
            
            // Print working set to console.
            Console::WriteLine( "Physical Memory Usage : {0}", myProcess->WorkingSet.ToString() );
            
            // Wait 2 seconds.
            Thread::Sleep( 2000 );
         }
         else
         {
            break;
         }

      }
      myProcess->CloseMainWindow();
      
      // Free resources associated with process.
      myProcess->Close();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The following exception was raised: " );
      Console::WriteLine( e->Message );
   }

}
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace ProcessSample
{
    class MyProcessClass
    {
        public static void Main()
        {
            try
            {
                using (Process myProcess = Process.Start("Notepad.exe"))
                {
                    // Display physical memory usage 5 times at intervals of 2 seconds.
                    for (int i = 0; i < 5; i++)
                    {
                        if (!myProcess.HasExited)
                        {
                            // Discard cached information about the process.
                            myProcess.Refresh();
                            // Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}");
                            // Wait 2 seconds.
                            Thread.Sleep(2000);
                        }
                        else
                        {
                            break;
                        }
                    }

                    // Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow();
                    // Free resources associated with process.
                    myProcess.Close();
                }
            }
            catch (Exception e) when (e is Win32Exception || e is FileNotFoundException)
            {
                Console.WriteLine("The following exception was raised: ");
                Console.WriteLine(e.Message);
            }
        }
    }
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading


try
    use myProcess = Process.Start "Notepad.exe"
    // Display physical memory usage 5 times at intervals of 2 seconds.
    let mutable i = 0

    while i < 5 && not myProcess.HasExited do
        // Discard cached information about the process.
        myProcess.Refresh()
        // Print working set to console.
        printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
        // Wait 2 seconds.
        Thread.Sleep 2000
        i <- i + 1
    // Close process by sending a close message to its main window.
    myProcess.CloseMainWindow() |> ignore
    // Free resources associated with process.
    myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
    printfn "The following exception was raised: "
    printfn $"{e.Message}"
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.IO
Imports System.Threading

Namespace Process_Sample
    Class MyProcessClass

        Public Shared Sub Main()
            Try
                Using myProcess = Process.Start("Notepad.exe")
                    ' Display physical memory usage 5 times at intervals of 2 seconds.
                    Dim i As Integer
                    For i = 0 To 4
                        If Not myProcess.HasExited Then

                            ' Discard cached information about the process.
                            myProcess.Refresh()
                            ' Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}")
                            ' Wait 2 seconds.
                            Thread.Sleep(2000)
                        Else
                            Exit For
                        End If

                    Next i

                    ' Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow()
                    ' Free resources associated with process.
                    myProcess.Close()
                End Using
            Catch e As Exception When TypeOf e Is Win32Exception Or TypeOf e Is FileNotFoundException
                Console.WriteLine("The following exception was raised: ")
                Console.WriteLine(e.Message)
            End Try
        End Sub
    End Class
End Namespace 'Process_Sample

Remarks

When a process is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system. Calling CloseMainWindow sends a request to close the main window, which, in a well-formed application, closes child windows and revokes all running message loops for the application. The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. To force the application to quit, use the Kill method. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.

Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination, and should be used only when necessary. CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. If CloseMainWindow fails, you can use Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces.

You can call Kill and CloseMainWindow only for processes that are running on the local computer. You cannot cause processes on remote computers to exit. You can only view information for processes running on remote computers.

Applies to