Process.CloseMainWindow 方法

定义

通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。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

返回

Boolean

如果成功发送了关闭消息,则为 true;如果关联进程没有主窗口或禁用了主窗口(例如,如果当前显示模式对话框),则为 falsetrue 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).

例外

已经退出该进程。The process has already exited.

-or- 没有与此 Process 对象关联的进程。No process is associated with this Process object.

示例

下面的示例启动记事本的实例。The following example starts an instance of Notepad. 然后,它以2秒的间隔检索关联进程的物理内存使用率,最大值为10秒。It then retrieves the physical memory usage of the associated process at 2 second intervals for a maximum of 10 seconds. 该示例检测进程是否在10秒后退出。The example detects whether the process exits before 10 seconds have elapsed. 如果10秒后仍在运行,则此示例将关闭进程。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);
            }
        }
    }
}
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

注解

进程执行时,其消息循环处于等待状态。When a process is executing, its message loop is in a wait state. 每次操作系统将 Windows 消息发送到该进程时,消息循环都会执行。The message loop executes every time a Windows message is sent to the process by the operating system. 调用 CloseMainWindow 将发送一个请求来关闭主窗口,该窗口在格式正确的应用程序中将关闭子窗口,并为应用程序撤消所有正在运行的消息循环。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. 通过调用退出进程的请求 CloseMainWindow 不会强制应用程序退出。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. 若要强制退出应用程序,请使用 Kill 方法。To force the application to quit, use the Kill method. 的行为与 CloseMainWindow 用户使用系统菜单关闭应用程序的主窗口的行为相同。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.

如果调用,则由进程编辑的数据或分配给进程的资源可能会丢失 KillData edited by the process or resources allocated to the process can be lost if you call Kill. Kill 导致异常的进程终止,只应在必要时使用。Kill causes an abnormal process termination, and should be used only when necessary. CloseMainWindow 启用进程的有序终止,并关闭所有窗口,因此更适合使用接口的应用程序。CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. 如果 CloseMainWindow 失败,可使用 Kill 终止进程。If CloseMainWindow fails, you can use Kill to terminate the process. Kill 是终止没有图形界面的进程的唯一方法。Kill is the only way to terminate processes that do not have graphical interfaces.

只能为在 Kill CloseMainWindow 本地计算机上运行的进程调用和。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.

适用于