Process.Refresh Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Membuang informasi apa pun tentang proses terkait yang telah di-cache di dalam komponen proses.
public:
void Refresh();
public void Refresh ();
member this.Refresh : unit -> unit
Public Sub Refresh ()
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
Setelah Refresh dipanggil, permintaan pertama untuk informasi tentang setiap properti menyebabkan komponen proses mendapatkan nilai baru dari proses terkait.
Process Ketika komponen dikaitkan dengan sumber daya proses, nilai Process properti segera diisi sesuai dengan status proses terkait. Jika informasi tentang proses terkait kemudian berubah, perubahan tersebut tidak tercermin dalam Process nilai cache komponen. Komponen Process adalah rekam jepret dari sumber daya proses pada saat mereka dikaitkan. Untuk melihat nilai saat ini untuk proses terkait, panggil Refresh metode .