How to: Refresh Process Component Properties
A Process component's properties are stored in a cache. The properties are used to store the property values of the process that the component is bound to.
The properties are not filled until you make the first property request from the process. At that time the component fills its cached properties with the property values of the process that it is bound to. The component does not renew this information when subsequent property value requests are made.
To view current property values, you must call the Refresh method of the Process component before requesting property values of the process. When you call the Refresh method, the values in the component's properties are replaced with the current data from the properties of the target process.
To refresh Process component properties
Create an instance of the process programmatically or by adding a Process component to the project in design view. For more information, see How to: Bind to Existing Processes.
If any changes have occurred to the process properties prior to retrieving property information, call the Refresh method.
Set properties to read the appropriate process information and assign the returned value to a variable.
The following example shows how to open an instance of Notepad and then calls the Refresh method to update the component's properties. The code then reads the updated WorkingSet64 property of the component to return the total amount of memory allocated for the associated process. The updated property value is then written to a console screen six times in two-second intervals. The console stays open for five seconds after the procedure is finished.
Dim Notepad As New Process() Dim i As Integer Notepad = Process.Start("Notepad.exe") For i = 0 To 5 ' Forces the Process component to get a new set of property values. Notepad.Refresh() ' Writes the property value to the console screen. Console.WriteLine(Notepad.WorkingSet64) ' Waits two seconds before running the next loop. System.Threading.Thread.Sleep(2000) Next ' Closes Notepad and waits 5 seconds before closing the console screen. Notepad.CloseMainWindow() System.Threading.Thread.Sleep(5000)
Process notepad; notepad = Process.Start("Notepad"); for (int i = 0; i < 5; i++) { // Forces the Process component to get a new set // of property values. notepad.Refresh(); // Writes the property value to the console screen. Console.WriteLine(notepad.WorkingSet64.ToString()); // Waits two seconds before running the next loop. System.Threading.Thread.Sleep(2000); } // Closes Notepad and waits 5 seconds before closing // the console screen. notepad.CloseMainWindow(); System.Threading.Thread.Sleep(5000);
See Also
Tasks
How to: Bind to Existing Processes
How to: View Running Processes
Concepts
Introduction to Monitoring and Managing Windows Processes