Process.Close メソッド

定義

このコンポーネントに関連付けられているすべてのリソースを解放します。Frees all the resources that are associated with this component.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

次の例では、メモ帳のインスタンスを起動します。The following example starts an instance of Notepad. 次に、関連付けられたプロセスの物理メモリ使用量を、最大で10秒間、2秒間隔で取得します。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

注釈

Close メソッドを使用すると、プロセスは待機している場合は終了を待機し、プロセスハンドルを閉じ、プロセス固有のプロパティをクリアします。The Close method causes the process to stop waiting for exit if it was waiting, closes the process handle, and clears process-specific properties. Close は、外部から参照されている場合に、標準出力、入力、およびエラーリーダーとライターを閉じません。Close does not close the standard output, input, and error readers and writers in case they are being referenced externally.

注意

Dispose メソッドは Close を呼び出します。The Dispose method calls Close. using ブロックに Process オブジェクトを配置すると、Closeを呼び出す必要なくリソースが解放されます。Placing the Process object in a using block disposes of resources without the need to call Close.

セキュリティ

LinkDemand
直前の呼び出し元に対する完全な信頼の場合。for full trust for the immediate caller. このメンバーは、部分的に信頼されているコードから使用することはできません。This member cannot be used by partially trusted code.

適用対象