Process.Refresh 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
捨棄已於進程元件內快取之相關聯進程的任何資訊。
public:
void Refresh();
public void Refresh();
member this.Refresh : unit -> unit
Public Sub Refresh ()
範例
以下範例啟動一個 Notepad 實例。 接著它會以 2 秒的間隔擷取相關程序的物理記憶體使用量,最多可持續 10 秒。 範例中判斷程序是否在 10 秒內退出。 範例中若程序在 10 秒後仍在運行,則會關閉程序。
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);
}
}
}
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading
try
use myProcess = Process.Start "Notepad.exe"
// Display physical memory usage 5 times at intervals of 2 seconds.
let mutable i = 0
while i < 5 && not myProcess.HasExited do
// Discard cached information about the process.
myProcess.Refresh()
// Print working set to console.
printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
// Wait 2 seconds.
Thread.Sleep 2000
i <- i + 1
// Close process by sending a close message to its main window.
myProcess.CloseMainWindow() |> ignore
// Free resources associated with process.
myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
printfn "The following exception was raised: "
printfn $"{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
備註
在 Refresh 呼叫 後,對每個屬性的首次請求會使程序元件從相關程序獲得新的值。
當元件 Process 與程序資源相關聯時,該 Process 元件的屬性值會立即根據相關程序的狀態被填充。 若相關程序的資訊隨後改變,這些變更不會反映在 Process 元件的快取值中。 元件 Process 是流程資源在關聯時的快照。 要查看相關程序的當前值,請呼叫該 Refresh 方法。