Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,15 @@ class ProcessMonitorSample
{
public static void Main()
{

// Define variables to track the peak
// memory usage of the process.
long peakPagedMem = 0,
peakWorkingSet = 0,
peakVirtualMem = 0;

Process myProcess = null;

try
// Start the process.
using (Process myProcess = Process.Start("NotePad.exe"))
{
// Start the process.
myProcess = Process.Start("NotePad.exe");

// Display the process statistics until
// the user closes the program.
do
Expand All @@ -40,25 +35,17 @@ public static void Main()

// Display current process statistics.

Console.WriteLine("{0} -", myProcess.ToString());
Console.WriteLine($"{myProcess} -");
Console.WriteLine("-------------------------------------");

Console.WriteLine(" physical memory usage: {0}",
myProcess.WorkingSet64);
Console.WriteLine(" base priority: {0}",
myProcess.BasePriority);
Console.WriteLine(" priority class: {0}",
myProcess.PriorityClass);
Console.WriteLine(" user processor time: {0}",
myProcess.UserProcessorTime);
Console.WriteLine(" privileged processor time: {0}",
myProcess.PrivilegedProcessorTime);
Console.WriteLine(" total processor time: {0}",
myProcess.TotalProcessorTime);
Console.WriteLine(" PagedSystemMemorySize64: {0}",
myProcess.PagedSystemMemorySize64);
Console.WriteLine(" PagedMemorySize64: {0}",
myProcess.PagedMemorySize64);
Console.WriteLine($" Physical memory usage : {myProcess.WorkingSet64}");
Console.WriteLine($" Base priority : {myProcess.BasePriority}");
Console.WriteLine($" Priority class : {myProcess.PriorityClass}");
Console.WriteLine($" User processor time : {myProcess.UserProcessorTime}");
Console.WriteLine($" Privileged processor time : {myProcess.PrivilegedProcessorTime}");
Console.WriteLine($" Total processor time : {myProcess.TotalProcessorTime}");
Console.WriteLine($" Paged system memory size : {myProcess.PagedSystemMemorySize64}");
Console.WriteLine($" Paged memory size : {myProcess.PagedMemorySize64}");

// Update the values for the overall peak memory statistics.
peakPagedMem = myProcess.PeakPagedMemorySize64;
Expand All @@ -79,27 +66,14 @@ public static void Main()


Console.WriteLine();
Console.WriteLine("Process exit code: {0}",
myProcess.ExitCode);
Console.WriteLine($"Process exit code: {myProcess.ExitCode}");

// Display peak memory statistics for the process.
Console.WriteLine("Peak physical memory usage of the process: {0}",
peakWorkingSet);
Console.WriteLine("Peak paged memory usage of the process: {0}",
peakPagedMem);
Console.WriteLine("Peak virtual memory usage of the process: {0}",
peakVirtualMem);

}
finally
{
if (myProcess != null)
{
myProcess.Close();
}
Console.WriteLine($"Peak physical memory usage: {peakWorkingSet}");
Console.WriteLine($"Peak paged memory usage: {peakPagedMem}");
Console.WriteLine($"Peak virtual memory usage: {peakVirtualMem}");
}
}

}
}
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ static void Main()
}
}
}
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ class MyProcess
{
public static void Main()
{
Process myProcess = new Process();

try
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
using (Process myProcess = new Process())
{
myProcess.StartInfo.UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// This code assumes the process you are starting will terminate itself.
// Given that is is started without a window so you cannot terminate it
// on the desktop, it must terminate itself or you can do it programmatically
// from this application using the Kill method.
}
}
catch (Exception e)
{
Expand All @@ -30,4 +31,4 @@ public static void Main()
}
}
}
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void OpenApplication(string myFavoritesPath)
// Display the contents of the favorites folder in the browser.
Process.Start(myFavoritesPath);
}

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
Expand Down Expand Up @@ -135,12 +135,12 @@ static void Main(string[] args)

for (var i = 0; i < args.Length; i++)
{
Console.WriteLine("[" + i + "] = " + args[i]);
Console.WriteLine($"[{i}] = {args[i]}");
}

Console.WriteLine("\nPress any key to exit");
Console.ReadLine();
}
}
}
// </Snippet3>
// </Snippet3>
103 changes: 52 additions & 51 deletions snippets/csharp/VS_Snippets_CLR/ProcessModule/CS/processmodule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,56 @@

class MyProcessModuleClass
{
public static void Main()
{
try
{
// <Snippet1>
Process myProcess = new Process();
// Get the process start information of notepad.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a notepad.
myProcess.Start();
System.Threading.Thread.Sleep(1000);
ProcessModule myProcessModule;
// Get all the modules associated with 'myProcess'.
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
Console.WriteLine("Properties of the modules associated "
+"with 'notepad' are:");
// Display the properties of each of the modules.
for( int i=0;i<myProcessModuleCollection.Count;i++)
{
myProcessModule = myProcessModuleCollection[i];
Console.WriteLine("The moduleName is "
+myProcessModule.ModuleName);
Console.WriteLine("The " +myProcessModule.ModuleName + "'s base address is: "
+myProcessModule.BaseAddress);
Console.WriteLine("The " +myProcessModule.ModuleName + "'s Entry point address is: "
+myProcessModule.EntryPointAddress);
Console.WriteLine("The " +myProcessModule.ModuleName + "'s File name is: "
+myProcessModule.FileName);
}
// Get the main module associated with 'myProcess'.
myProcessModule = myProcess.MainModule;
// Display the properties of the main module.
Console.WriteLine("The process's main moduleName is: "
+myProcessModule.ModuleName);
Console.WriteLine("The process's main module's base address is: "
+myProcessModule.BaseAddress);
Console.WriteLine("The process's main module's Entry point address is: "
+myProcessModule.EntryPointAddress);
Console.WriteLine("The process's main module's File name is: "
+myProcessModule.FileName);
myProcess.CloseMainWindow();
// </Snippet1>
}
catch(Exception e)
{
Console.WriteLine("Exception : "+ e.Message);
}
}
public static void Main()
{
try
{
// <Snippet1>
using (Process myProcess = new Process())
{
// Get the process start information of notepad.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a notepad.
myProcess.Start();
System.Threading.Thread.Sleep(1000);
ProcessModule myProcessModule;
// Get all the modules associated with 'myProcess'.
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
Console.WriteLine("Properties of the modules associated "
+ "with 'notepad' are:");
// Display the properties of each of the modules.
for (int i = 0; i < myProcessModuleCollection.Count; i++)
{
myProcessModule = myProcessModuleCollection[i];
Console.WriteLine("The moduleName is "
+ myProcessModule.ModuleName);
Console.WriteLine("The " + myProcessModule.ModuleName + "'s base address is: "
+ myProcessModule.BaseAddress);
Console.WriteLine("The " + myProcessModule.ModuleName + "'s Entry point address is: "
+ myProcessModule.EntryPointAddress);
Console.WriteLine("The " + myProcessModule.ModuleName + "'s File name is: "
+ myProcessModule.FileName);
}
// Get the main module associated with 'myProcess'.
myProcessModule = myProcess.MainModule;
// Display the properties of the main module.
Console.WriteLine("The process's main moduleName is: "
+ myProcessModule.ModuleName);
Console.WriteLine("The process's main module's base address is: "
+ myProcessModule.BaseAddress);
Console.WriteLine("The process's main module's Entry point address is: "
+ myProcessModule.EntryPointAddress);
Console.WriteLine("The process's main module's File name is: "
+ myProcessModule.FileName);
myProcess.CloseMainWindow();
}
// </Snippet1>
}
catch (Exception e)
{
Console.WriteLine("Exception : " + e.Message);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,44 @@

class MyProcessModuleClass
{
public static void Main()
{
try
{
// <Snippet1>
Process myProcess = new Process();
// Get the process start information of notepad.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a notepad.
myProcess.Start();
System.Threading.Thread.Sleep(1000);
ProcessModule myProcessModule;
// Get all the modules associated with 'myProcess'.
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
Console.WriteLine("Base addresses of the modules associated "
+"with 'notepad' are:");
// Display the 'BaseAddress' of each of the modules.
for( int i = 0; i < myProcessModuleCollection.Count; i++)
{
myProcessModule = myProcessModuleCollection[i];
Console.WriteLine(myProcessModule.ModuleName+" : "
+myProcessModule.BaseAddress);
}
// Get the main module associated with 'myProcess'.
myProcessModule = myProcess.MainModule;
// Display the 'BaseAddress' of the main module.
Console.WriteLine("The process's main module's base address is: "
+myProcessModule.BaseAddress);
myProcess.CloseMainWindow();
// </Snippet1>
}
catch(Exception e)
{
Console.WriteLine("Exception : "+ e.Message);
}
}
public static void Main()
{
try
{
// <Snippet1>
using (Process myProcess = new Process())
{
// Get the process start information of notepad.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
myProcess.StartInfo = myProcessStartInfo;
// Create a notepad.
myProcess.Start();
System.Threading.Thread.Sleep(1000);
ProcessModule myProcessModule;
// Get all the modules associated with 'myProcess'.
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
Console.WriteLine("Base addresses of the modules associated "
+ "with 'notepad' are:");
// Display the 'BaseAddress' of each of the modules.
for (int i = 0; i < myProcessModuleCollection.Count; i++)
{
myProcessModule = myProcessModuleCollection[i];
Console.WriteLine(myProcessModule.ModuleName + " : "
+ myProcessModule.BaseAddress);
}
// Get the main module associated with 'myProcess'.
myProcessModule = myProcess.MainModule;
// Display the 'BaseAddress' of the main module.
Console.WriteLine("The process's main module's base address is: "
+ myProcessModule.BaseAddress);
myProcess.CloseMainWindow();
}
// </Snippet1>
}
catch (Exception e)
{
Console.WriteLine("Exception : " + e.Message);
}
}
}

Loading