次の方法で共有


Process.CloseMainWindow メソッド

メイン ウィンドウにクローズ メッセージを送信して、ユーザー インターフェイスがあるプロセスを終了します。

Public Function CloseMainWindow() As Boolean
[C#]
public bool CloseMainWindow();
[C++]
public: bool CloseMainWindow();
[JScript]
public function CloseMainWindow() : Boolean;

戻り値

クローズ メッセージが正常に送信された場合は true 。関連付けられたプロセスにメイン ウィンドウがない場合、またはメイン ウィンドウが無効の場合 (モーダル ダイアログ ボックスが表示されているときなど) は false

解説

プロセスの実行中、メッセージ ループは待機状態です。メッセージ ループは、オペレーティング システムによって Windows メッセージがプロセスに送信されるたびに実行されます。 CloseMainWindow を呼び出すと、メイン ウィンドウを閉じる要求が送信されます。適切に構成されたアプリケーションでは、これにより子ウィンドウも閉じられ、そのアプリケーションのために実行されているすべてのメッセージ ループが終了します。 CloseMainWindow を呼び出してプロセスの終了を要求しても、アプリケーションは強制終了されません。アプリケーションでは、終了前にユーザーに確認を求めたり、終了を拒否できます。アプリケーションを強制的に終了するには、 Kill メソッドを使用します。 CloseMainWindow の動作は、システム メニューを使用してアプリケーションのメイン ウィンドウを閉じるときと同じです。そのため、メイン ウィンドウを閉じてプロセスの終了を要求しても、アプリケーションはすぐには強制的に終了されません。

プロセスによって編集されたデータまたはプロセスに割り当てられたリソースは、 Kill を呼び出すと失われることがあります。 Kill はプロセスの異常終了を発生させるため、必要なときにだけ使用してください。 CloseMainWindow を使用すると、プロセスを手順に従って終了させ、すべてのウィンドウを閉じることができるため、インターフェイスのあるアプリケーションにはより適切です。 CloseMainWindow が失敗した場合は、 Kill を使用してプロセスを終了できます。グラフィカル インターフェイスのないプロセスを終了させるには、 Kill が唯一の方法です。

KillCloseMainWindow を呼び出すことができるのは、ローカル コンピュータで実行されているプロセスだけです。リモート コンピュータ上のプロセスは終了できません。リモート コンピュータ上で実行中のプロセスは、情報の表示だけができます。

使用例

[Visual Basic, C#, C++] メモ帳のインスタンスを起動する例を次に示します。このコードは、インスタンスが起動すると、2 秒間隔で最大 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

[C#] 

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

[C++] 
#using <mscorlib.dll>
#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(S"Physical Memory Usage : {0}", 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(S"The following exception was raised: ");
       Console::WriteLine(e->Message);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

Process クラス | Process メンバ | System.Diagnostics 名前空間