Process.Refresh メソッド

定義

プロセス コンポーネントにキャッシュされている関連付けられたプロセスに関するすべての情報を破棄します。

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

次の例では、メモ帳のインスタンスを開始します。 次に、関連付けられているプロセスの物理メモリ使用量を 2 秒間隔で最大 10 秒間取得します。 この例では、10 秒が経過する前にプロセスが終了するかどうかを検出します。 この例では、10 秒後も実行されている場合は、プロセスを閉じます。

#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

注釈

呼び出された後 Refresh 、各プロパティに関する情報を最初に要求すると、プロセス コンポーネントは関連付けられたプロセスから新しい値を取得します。

コンポーネントが Process プロセス リソースに関連付けられている場合、関連付けられているプロセスの Process 状態に応じて、そのコンポーネントのプロパティ値が直ちに設定されます。 関連付けられたプロセスに関する情報が後で変更された場合、それらの変更はコンポーネントのキャッシュされた値には Process 反映されません。 コンポーネントは Process 、関連付けられている時点でのプロセス リソースのスナップショットです。 関連付けられているプロセスの現在の値を表示するには、メソッドを Refresh 呼び出します。

適用対象