Skip to content

Commit 25fe3e3

Browse files
nxtnBillWagner
authored andcommitted
Dispose IDisposable objects (#545)
* Dispose IDisposable objects * Format codes * Use string interpolation * Update source.cs * Use string interpolation and remove redundancy * More * Update source.cs * Update process_getprocessesbyname2_2.cs * Update process_sample.cs * Update processexitedevent.cs * Update source.cs * Update process_getprocessesbyname2_2.cs * Update process_sample.cs * Update processexitedevent.cs
1 parent 6010947 commit 25fe3e3

File tree

23 files changed

+722
-741
lines changed

23 files changed

+722
-741
lines changed

snippets/csharp/VS_Snippets_CLR/Diag_Process_MemoryProperties64/CS/source.cs

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,15 @@ class ProcessMonitorSample
1313
{
1414
public static void Main()
1515
{
16-
1716
// Define variables to track the peak
1817
// memory usage of the process.
1918
long peakPagedMem = 0,
2019
peakWorkingSet = 0,
2120
peakVirtualMem = 0;
2221

23-
Process myProcess = null;
24-
25-
try
22+
// Start the process.
23+
using (Process myProcess = Process.Start("NotePad.exe"))
2624
{
27-
// Start the process.
28-
myProcess = Process.Start("NotePad.exe");
29-
3025
// Display the process statistics until
3126
// the user closes the program.
3227
do
@@ -40,25 +35,17 @@ public static void Main()
4035

4136
// Display current process statistics.
4237

43-
Console.WriteLine("{0} -", myProcess.ToString());
38+
Console.WriteLine($"{myProcess} -");
4439
Console.WriteLine("-------------------------------------");
4540

46-
Console.WriteLine(" physical memory usage: {0}",
47-
myProcess.WorkingSet64);
48-
Console.WriteLine(" base priority: {0}",
49-
myProcess.BasePriority);
50-
Console.WriteLine(" priority class: {0}",
51-
myProcess.PriorityClass);
52-
Console.WriteLine(" user processor time: {0}",
53-
myProcess.UserProcessorTime);
54-
Console.WriteLine(" privileged processor time: {0}",
55-
myProcess.PrivilegedProcessorTime);
56-
Console.WriteLine(" total processor time: {0}",
57-
myProcess.TotalProcessorTime);
58-
Console.WriteLine(" PagedSystemMemorySize64: {0}",
59-
myProcess.PagedSystemMemorySize64);
60-
Console.WriteLine(" PagedMemorySize64: {0}",
61-
myProcess.PagedMemorySize64);
41+
Console.WriteLine($" Physical memory usage : {myProcess.WorkingSet64}");
42+
Console.WriteLine($" Base priority : {myProcess.BasePriority}");
43+
Console.WriteLine($" Priority class : {myProcess.PriorityClass}");
44+
Console.WriteLine($" User processor time : {myProcess.UserProcessorTime}");
45+
Console.WriteLine($" Privileged processor time : {myProcess.PrivilegedProcessorTime}");
46+
Console.WriteLine($" Total processor time : {myProcess.TotalProcessorTime}");
47+
Console.WriteLine($" Paged system memory size : {myProcess.PagedSystemMemorySize64}");
48+
Console.WriteLine($" Paged memory size : {myProcess.PagedMemorySize64}");
6249

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

8067

8168
Console.WriteLine();
82-
Console.WriteLine("Process exit code: {0}",
83-
myProcess.ExitCode);
69+
Console.WriteLine($"Process exit code: {myProcess.ExitCode}");
8470

8571
// Display peak memory statistics for the process.
86-
Console.WriteLine("Peak physical memory usage of the process: {0}",
87-
peakWorkingSet);
88-
Console.WriteLine("Peak paged memory usage of the process: {0}",
89-
peakPagedMem);
90-
Console.WriteLine("Peak virtual memory usage of the process: {0}",
91-
peakVirtualMem);
92-
93-
}
94-
finally
95-
{
96-
if (myProcess != null)
97-
{
98-
myProcess.Close();
99-
}
72+
Console.WriteLine($"Peak physical memory usage: {peakWorkingSet}");
73+
Console.WriteLine($"Peak paged memory usage: {peakPagedMem}");
74+
Console.WriteLine($"Peak virtual memory usage: {peakVirtualMem}");
10075
}
10176
}
102-
10377
}
10478
}
105-
// </Snippet1>
79+
// </Snippet1>

snippets/csharp/VS_Snippets_CLR/Process.GetProcesses_noexception/CS/processstaticget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ static void Main()
4848
}
4949
}
5050
}
51-
// </Snippet1>
51+
// </Snippet1>

snippets/csharp/VS_Snippets_CLR/Process.Start_instance/CS/processstart.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@ class MyProcess
99
{
1010
public static void Main()
1111
{
12-
Process myProcess = new Process();
13-
1412
try
1513
{
16-
myProcess.StartInfo.UseShellExecute = false;
17-
// You can start any process, HelloWorld is a do-nothing example.
18-
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
19-
myProcess.StartInfo.CreateNoWindow = true;
20-
myProcess.Start();
21-
// This code assumes the process you are starting will terminate itself.
22-
// Given that is is started without a window so you cannot terminate it
23-
// on the desktop, it must terminate itself or you can do it programmatically
24-
// from this application using the Kill method.
14+
using (Process myProcess = new Process())
15+
{
16+
myProcess.StartInfo.UseShellExecute = false;
17+
// You can start any process, HelloWorld is a do-nothing example.
18+
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
19+
myProcess.StartInfo.CreateNoWindow = true;
20+
myProcess.Start();
21+
// This code assumes the process you are starting will terminate itself.
22+
// Given that is is started without a window so you cannot terminate it
23+
// on the desktop, it must terminate itself or you can do it programmatically
24+
// from this application using the Kill method.
25+
}
2526
}
2627
catch (Exception e)
2728
{
@@ -30,4 +31,4 @@ public static void Main()
3031
}
3132
}
3233
}
33-
// </Snippet1>
34+
// </Snippet1>

snippets/csharp/VS_Snippets_CLR/Process.Start_static/CS/processstartstatic.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void OpenApplication(string myFavoritesPath)
1616
// Display the contents of the favorites folder in the browser.
1717
Process.Start(myFavoritesPath);
1818
}
19-
19+
2020
// Opens urls and .html documents using Internet Explorer.
2121
void OpenWithArguments()
2222
{
@@ -135,12 +135,12 @@ static void Main(string[] args)
135135

136136
for (var i = 0; i < args.Length; i++)
137137
{
138-
Console.WriteLine("[" + i + "] = " + args[i]);
138+
Console.WriteLine($"[{i}] = {args[i]}");
139139
}
140-
140+
141141
Console.WriteLine("\nPress any key to exit");
142142
Console.ReadLine();
143143
}
144144
}
145145
}
146-
// </Snippet3>
146+
// </Snippet3>

snippets/csharp/VS_Snippets_CLR/ProcessModule/CS/processmodule.cs

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,56 @@
99

1010
class MyProcessModuleClass
1111
{
12-
public static void Main()
13-
{
14-
try
15-
{
16-
// <Snippet1>
17-
Process myProcess = new Process();
18-
// Get the process start information of notepad.
19-
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
20-
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
21-
myProcess.StartInfo = myProcessStartInfo;
22-
// Create a notepad.
23-
myProcess.Start();
24-
System.Threading.Thread.Sleep(1000);
25-
ProcessModule myProcessModule;
26-
// Get all the modules associated with 'myProcess'.
27-
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
28-
Console.WriteLine("Properties of the modules associated "
29-
+"with 'notepad' are:");
30-
// Display the properties of each of the modules.
31-
for( int i=0;i<myProcessModuleCollection.Count;i++)
32-
{
33-
myProcessModule = myProcessModuleCollection[i];
34-
Console.WriteLine("The moduleName is "
35-
+myProcessModule.ModuleName);
36-
Console.WriteLine("The " +myProcessModule.ModuleName + "'s base address is: "
37-
+myProcessModule.BaseAddress);
38-
Console.WriteLine("The " +myProcessModule.ModuleName + "'s Entry point address is: "
39-
+myProcessModule.EntryPointAddress);
40-
Console.WriteLine("The " +myProcessModule.ModuleName + "'s File name is: "
41-
+myProcessModule.FileName);
42-
}
43-
// Get the main module associated with 'myProcess'.
44-
myProcessModule = myProcess.MainModule;
45-
// Display the properties of the main module.
46-
Console.WriteLine("The process's main moduleName is: "
47-
+myProcessModule.ModuleName);
48-
Console.WriteLine("The process's main module's base address is: "
49-
+myProcessModule.BaseAddress);
50-
Console.WriteLine("The process's main module's Entry point address is: "
51-
+myProcessModule.EntryPointAddress);
52-
Console.WriteLine("The process's main module's File name is: "
53-
+myProcessModule.FileName);
54-
myProcess.CloseMainWindow();
55-
// </Snippet1>
56-
}
57-
catch(Exception e)
58-
{
59-
Console.WriteLine("Exception : "+ e.Message);
60-
}
61-
}
12+
public static void Main()
13+
{
14+
try
15+
{
16+
// <Snippet1>
17+
using (Process myProcess = new Process())
18+
{
19+
// Get the process start information of notepad.
20+
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
21+
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
22+
myProcess.StartInfo = myProcessStartInfo;
23+
// Create a notepad.
24+
myProcess.Start();
25+
System.Threading.Thread.Sleep(1000);
26+
ProcessModule myProcessModule;
27+
// Get all the modules associated with 'myProcess'.
28+
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
29+
Console.WriteLine("Properties of the modules associated "
30+
+ "with 'notepad' are:");
31+
// Display the properties of each of the modules.
32+
for (int i = 0; i < myProcessModuleCollection.Count; i++)
33+
{
34+
myProcessModule = myProcessModuleCollection[i];
35+
Console.WriteLine("The moduleName is "
36+
+ myProcessModule.ModuleName);
37+
Console.WriteLine("The " + myProcessModule.ModuleName + "'s base address is: "
38+
+ myProcessModule.BaseAddress);
39+
Console.WriteLine("The " + myProcessModule.ModuleName + "'s Entry point address is: "
40+
+ myProcessModule.EntryPointAddress);
41+
Console.WriteLine("The " + myProcessModule.ModuleName + "'s File name is: "
42+
+ myProcessModule.FileName);
43+
}
44+
// Get the main module associated with 'myProcess'.
45+
myProcessModule = myProcess.MainModule;
46+
// Display the properties of the main module.
47+
Console.WriteLine("The process's main moduleName is: "
48+
+ myProcessModule.ModuleName);
49+
Console.WriteLine("The process's main module's base address is: "
50+
+ myProcessModule.BaseAddress);
51+
Console.WriteLine("The process's main module's Entry point address is: "
52+
+ myProcessModule.EntryPointAddress);
53+
Console.WriteLine("The process's main module's File name is: "
54+
+ myProcessModule.FileName);
55+
myProcess.CloseMainWindow();
56+
}
57+
// </Snippet1>
58+
}
59+
catch (Exception e)
60+
{
61+
Console.WriteLine("Exception : " + e.Message);
62+
}
63+
}
6264
}
63-

snippets/csharp/VS_Snippets_CLR/ProcessModule_BaseAddress/CS/processmodule_baseaddress.cs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,44 @@
1010

1111
class MyProcessModuleClass
1212
{
13-
public static void Main()
14-
{
15-
try
16-
{
17-
// <Snippet1>
18-
Process myProcess = new Process();
19-
// Get the process start information of notepad.
20-
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
21-
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
22-
myProcess.StartInfo = myProcessStartInfo;
23-
// Create a notepad.
24-
myProcess.Start();
25-
System.Threading.Thread.Sleep(1000);
26-
ProcessModule myProcessModule;
27-
// Get all the modules associated with 'myProcess'.
28-
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
29-
Console.WriteLine("Base addresses of the modules associated "
30-
+"with 'notepad' are:");
31-
// Display the 'BaseAddress' of each of the modules.
32-
for( int i = 0; i < myProcessModuleCollection.Count; i++)
33-
{
34-
myProcessModule = myProcessModuleCollection[i];
35-
Console.WriteLine(myProcessModule.ModuleName+" : "
36-
+myProcessModule.BaseAddress);
37-
}
38-
// Get the main module associated with 'myProcess'.
39-
myProcessModule = myProcess.MainModule;
40-
// Display the 'BaseAddress' of the main module.
41-
Console.WriteLine("The process's main module's base address is: "
42-
+myProcessModule.BaseAddress);
43-
myProcess.CloseMainWindow();
44-
// </Snippet1>
45-
}
46-
catch(Exception e)
47-
{
48-
Console.WriteLine("Exception : "+ e.Message);
49-
}
50-
}
13+
public static void Main()
14+
{
15+
try
16+
{
17+
// <Snippet1>
18+
using (Process myProcess = new Process())
19+
{
20+
// Get the process start information of notepad.
21+
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe");
22+
// Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object.
23+
myProcess.StartInfo = myProcessStartInfo;
24+
// Create a notepad.
25+
myProcess.Start();
26+
System.Threading.Thread.Sleep(1000);
27+
ProcessModule myProcessModule;
28+
// Get all the modules associated with 'myProcess'.
29+
ProcessModuleCollection myProcessModuleCollection = myProcess.Modules;
30+
Console.WriteLine("Base addresses of the modules associated "
31+
+ "with 'notepad' are:");
32+
// Display the 'BaseAddress' of each of the modules.
33+
for (int i = 0; i < myProcessModuleCollection.Count; i++)
34+
{
35+
myProcessModule = myProcessModuleCollection[i];
36+
Console.WriteLine(myProcessModule.ModuleName + " : "
37+
+ myProcessModule.BaseAddress);
38+
}
39+
// Get the main module associated with 'myProcess'.
40+
myProcessModule = myProcess.MainModule;
41+
// Display the 'BaseAddress' of the main module.
42+
Console.WriteLine("The process's main module's base address is: "
43+
+ myProcessModule.BaseAddress);
44+
myProcess.CloseMainWindow();
45+
}
46+
// </Snippet1>
47+
}
48+
catch (Exception e)
49+
{
50+
Console.WriteLine("Exception : " + e.Message);
51+
}
52+
}
5153
}
52-

0 commit comments

Comments
 (0)