Process.HasExited Properti

Definisi

Mendapatkan nilai yang menunjukkan apakah proses terkait telah dihentikan.

public:
 property bool HasExited { bool get(); };
public bool HasExited { get; }
[System.ComponentModel.Browsable(false)]
public bool HasExited { get; }
member this.HasExited : bool
[<System.ComponentModel.Browsable(false)>]
member this.HasExited : bool
Public ReadOnly Property HasExited As Boolean

Nilai Properti

true jika proses sistem operasi yang dirujuk oleh Process komponen telah dihentikan; jika tidak, false.

Atribut

Pengecualian

Tidak ada proses yang terkait dengan objek.

Kode keluar untuk proses tidak dapat diambil.

Anda mencoba mengakses HasExited properti untuk proses yang berjalan pada komputer jarak jauh. Properti ini hanya tersedia untuk proses yang berjalan pada komputer lokal.

Contoh

Contoh berikut memulai instans Notepad. Kemudian mengambil penggunaan memori fisik dari proses terkait pada interval 2 detik selama maksimum 10 detik. Contoh mendeteksi apakah proses keluar sebelum 10 detik telah berlalu. Contoh menutup proses jika masih berjalan setelah 10 detik.

#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

Keterangan

Nilai true untuk HasExited menunjukkan bahwa proses terkait telah dihentikan, baik secara normal atau tidak normal. Anda dapat meminta atau memaksa proses terkait untuk keluar dengan memanggil CloseMainWindow atau Kill. Jika handel terbuka untuk proses, sistem operasi melepaskan memori proses ketika proses telah keluar, tetapi mempertahankan informasi administratif tentang proses, seperti handel, kode keluar, dan waktu keluar. Untuk mendapatkan informasi ini, Anda bisa menggunakan ExitCode properti dan ExitTime . Properti ini diisi secara otomatis untuk proses yang dimulai oleh komponen ini. Informasi administratif dirilis ketika semua Process komponen yang terkait dengan proses sistem dihancurkan dan tidak memegang lebih banyak handel untuk proses yang keluar.

Proses dapat berakhir secara independen dari kode Anda. Jika Anda memulai proses menggunakan komponen ini, sistem memperbarui nilai HasExited secara otomatis, bahkan jika proses terkait keluar secara independen.

Catatan

Ketika output standar telah dialihkan ke penanganan aktivitas asinkron, ada kemungkinan bahwa pemrosesan output tidak akan selesai ketika properti ini mengembalikan true. Untuk memastikan bahwa penanganan peristiwa asinkron telah selesai, panggil WaitForExit() kelebihan beban yang tidak membutuhkan parameter sebelum memeriksa HasExited.

Berlaku untuk

Lihat juga