Process.CloseMainWindow 方法

通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。

**命名空间:**System.Diagnostics
**程序集:**System(在 system.dll 中)

语法

声明
Public Function CloseMainWindow As Boolean
用法
Dim instance As Process
Dim returnValue As Boolean

returnValue = instance.CloseMainWindow
public bool CloseMainWindow ()
public:
bool CloseMainWindow ()
public boolean CloseMainWindow ()
public function CloseMainWindow () : boolean

返回值

如果成功发送了关闭消息,则为 true;如果关联进程没有主窗口或禁用了主窗口(例如,如果当前显示模式对话框),则为 false

异常

异常类型 条件

PlatformNotSupportedException

该平台为 Windows 98 或 Windows Millennium Edition (Windows Me);如果将 ProcessStartInfo.UseShellExecute 属性设置为 false,则可以在 Windows 98 和 Windows Me 上访问此属性。

备注

执行进程时,其消息循环处于等待状态。每当操作系统向该进程发送 Windows 消息时,该消息循环执行。调用 CloseMainWindow 会向主窗口发送关闭请求,在一个格式良好的应用程序中,该请求会关闭子窗口并撤消此应用程序所有正在运行的消息循环。通过调用 CloseMainWindow 发出的退出进程的请求不强制应用程序退出。应用程序可以在退出前请求用户验证,也可以拒绝退出。若要强制应用程序退出,请使用 Kill 方法。CloseMainWindow 的行为与用户使用系统菜单关闭应用程序主窗口的行为一样。因此,通过关闭主窗口发出的退出进程的请求不强制应用程序立即退出。

如果调用 Kill,则可能丢失该进程编辑的数据或分配给该进程的资源。Kill 将导致进程不正常终止,因而只应在必要时使用。CloseMainWindow 使进程能够有序地终止并关闭所有窗口,因而对于拥有界面的应用程序,使用它更好。如果 CloseMainWindow 失败,则可以使用 Kill 终止进程。Kill 是终止没有图形化界面的进程的唯一方法。

只能对在本地计算机上运行的进程调用 KillCloseMainWindow。无法使远程计算机上的进程退出。仅可查看在远程计算机上运行的进程的信息。

Windows 98, Windows Millennium Edition 平台说明: 如果在启动进程时 ProcessStartInfo.UseShellExecute 设置为 true,则此属性在此平台上不可用。

示例

下面的示例启动一个记事本实例。然后它最多在 10 秒内,以两秒为间隔检索关联进程的物理内存使用情况。该示例检测该进程在经过 10 秒后是否退出。如果该进程在 10 秒后仍在运行,该示例就会将其关闭。

Imports System
Imports System.Diagnostics
Imports System.Threading

Namespace Process_Sample
   Class MyProcessClass

      Public Shared Sub Main()
         Try

            Dim myProcess As Process
            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.ToString())
                  ' 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()

         Catch e As Exception
            Console.WriteLine("The following exception was raised: ")
            Console.WriteLine(e.Message)
         End Try
      End Sub 'Main
   End Class 'MyProcessClass
End Namespace 'Process_Sample
using System;
using System.Diagnostics;
using System.Threading;

namespace Process_Sample
{
   class MyProcessClass
   {
      public static void 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: " 
                                        + myProcess.WorkingSet.ToString());
                   // 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)
         {
            Console.WriteLine("The following exception was raised: ");
            Console.WriteLine(e.Message);
         }
      }
   }
}
#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 );
   }

}

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

Process 类
Process 成员
System.Diagnostics 命名空间