Skip to content

Commit dff70fa

Browse files
committed
Update MainWindow.xaml.cs
1 parent 0cfc1be commit dff70fa

1 file changed

Lines changed: 127 additions & 12 deletions

File tree

MainWindow.xaml.cs

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,109 +7,195 @@
77
using System.Threading.Tasks;
88
using System.Windows;
99

10+
/*
11+
* Primary namespace for the application
12+
* It includes the MainWindow class which is the main UI window of the application
13+
* and additional UI for each featuring
14+
*/
1015
namespace GetWMIBasic
1116
{
17+
/*
18+
* Main Window class
19+
* It is used to display the main UI of the application
20+
*/
21+
1222
public partial class MainWindow : Window
1323
{
24+
////////////////////////////////////////////////////////////////
25+
/// Global Properties and Constructors Region
26+
/// This region contains global properties and constructors
27+
/// for the MachineMethods class.
28+
////////////////////////////////////////////////////////////////
29+
1430
/*
15-
* Global properties
31+
* Global properties
32+
* machine: An instance of the MachineMethods class to perform WMI operations
1633
*/
1734
protected MachineMethods machine;
1835

36+
/*
37+
* Constructor of the MainWindow class
38+
* Initializes the components and sets up event handlers
39+
*/
1940
public MainWindow()
2041
{
42+
// Initialize components
2143
InitializeComponent();
44+
45+
// Initialize the MachineMethods instance for local machine
2246
machine = new MachineMethods();
47+
48+
// Set up the sync loading event handler. See the method below for further info.
2349
Loaded += MainWindow_Loaded;
2450
}
25-
51+
52+
/*
53+
* Main Windows async loading method.
54+
* It is used prevent long loading times on the UI thread
55+
*/
2656
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
2757
{
58+
// Call the Refresh method to load initial data
2859
await Refresh();
2960
}
3061

31-
/*
32-
* Button-based methods
62+
////////////////////////////////////////////////////////////////
63+
/// User Action Methods Region
64+
/// This region contains methods that handle user actions.
65+
/// For example, button clicks, changes in order.
66+
////////////////////////////////////////////////////////////////
67+
68+
/*
69+
* Actions for button "Connect to Another Computer"
3370
*/
3471
private async void ConnectToAnotherComputer_Button(object sender, RoutedEventArgs e)
3572
{
73+
// Initialize the ConnectForm instance
3674
ConnectForm connectForm = new ConnectForm();
3775

76+
// Get the user credentials from the ConnectForm
3877
(string computerName, string username, SecureString password) userCredential = connectForm.ReturnValue();
3978

79+
// If the computer name is not empty, create a new MachineMethods instance
4080
if (userCredential.computerName != "")
4181
{
42-
machine = null;
4382
machine = !userCredential.computerName.Equals("localhost") ? new MachineMethods(userCredential) : new MachineMethods();
44-
45-
await Refresh();
4683
}
84+
85+
// Refresh the data on the UI
86+
await Refresh();
4787
}
4888

89+
/*
90+
* Actons for button "Exit Application"
91+
*/
4992
private void Exit_Button(object sender, RoutedEventArgs e)
5093
{
94+
// Shut down the application
5195
Application.Current.Shutdown();
5296
}
5397

98+
/*
99+
*
100+
*/
54101
private async void Restart_Button(object sender, RoutedEventArgs e)
55102
{
103+
// Confirm the restart action with the user
56104
if (MessageBox.Show($"Do you want to restart the computer {machine.GetComputerName()}?", "Proceed?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
57105
{
106+
// Update the status bar to indicate loading
58107
StatusBarChange("Loading", true);
59108

109+
/*
110+
* Try to restart the remote machine using WMI, with the target computer name
111+
* Catch the exception and display it using the ExceptionView if any error occurs
112+
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
113+
*/
60114
try
61115
{
116+
// Since the local machine cannot be restarted remotely, throw an exception if the target is localhost
117+
// It should be done via the user context menu instead
62118
if (machine.GetComputerName() == "localhost")
63119
{
64120
throw new ManagementException("Local Host cannot be restarted");
65121
}
66122

123+
// Connect to the WMI namespace
67124
machine.Connect("root\\cimv2");
125+
126+
// Call the Win32Shutdown method with the restart flag (6)
68127
await machine.CallMethod("Win32_OperatingSystem", "*", "Win32Shutdown", new object[] { 6 });
69128
}
70129
catch (Exception ex)
71130
{
72-
exView.HandleException(ex);
131+
// Handle any exceptions using the ExceptionView
132+
(new ExceptionView()).HandleException(ex);
73133
}
74134
finally
75135
{
136+
// Update the status bar to indicate completion
76137
StatusBarChange("Done", false);
77138
}
78139
}
79140
}
80141

142+
/*
143+
* Actions for button "Shutdown Computer"
144+
*/
81145
private async void Shutdown_Button(object sender, RoutedEventArgs e)
82146
{
147+
// Confirm the shutdown action with the user
83148
if (MessageBox.Show($"Do you want to shutdown the computer {machine.GetComputerName()}?", "Proceed?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
84149
{
150+
// Update the status bar to indicate loading
85151
StatusBarChange("Loading", true);
86152

153+
/*
154+
* Try to shutdown the remote machine using WMI, with the target computer name
155+
* Catch the exception and display it using the ExceptionView if any error occurs
156+
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
157+
*/
87158
try
88159
{
160+
// Since the local machine cannot be shutdown remotely, throw an exception if the target is localhost
161+
// It should be done via the user context menu instead
89162
if (machine.GetComputerName() == "localhost")
90163
{
91164
throw new ManagementException("Local Host cannot be shut down");
92165
}
93166

167+
// Connect to the WMI namespace
94168
machine.Connect("root\\cimv2");
169+
170+
// Call the Win32Shutdown method with the shutdown flag (5)
95171
await machine.CallMethod("Win32_OperatingSystem", "*", "Win32Shutdown", new object[] { 5 });
96172
}
97173
catch (Exception ex)
98174
{
175+
// Handle any exceptions using the ExceptionView
99176
(new ExceptionView()).HandleException(ex);
100177
}
101178
finally
102179
{
180+
// Update the status bar to indicate completion
103181
StatusBarChange("Done", false);
104182
}
105183
}
106184
}
107185

186+
/*
187+
* Actions for button "Refresh Information"
188+
*/
108189
private async void Refresh_Button(object sender, RoutedEventArgs e)
109190
{
191+
// Call the Refresh method to update the data on the UI
110192
await Refresh();
111193
}
112194

195+
/*
196+
* Actions for button "Regular Exception Example"
197+
* (It will throw and catch a common exception to demonstrate the ExceptionView)
198+
*/
113199
private void RegularException_Click(object sender, RoutedEventArgs e)
114200
{
115201
try
@@ -118,10 +204,14 @@ private void RegularException_Click(object sender, RoutedEventArgs e)
118204
}
119205
catch (Exception ex)
120206
{
121-
exView.HandleException(ex);
207+
(new ExceptionView()).HandleException(ex);
122208
}
123209
}
124210

211+
/*
212+
* Actions for button "Critical Exception Example"
213+
* (It will throw and catch a critical exception to demonstrate the ExceptionView)
214+
*/
125215
private void CriticalException_Click(object sender, RoutedEventArgs e)
126216
{
127217
try
@@ -130,21 +220,39 @@ private void CriticalException_Click(object sender, RoutedEventArgs e)
130220
}
131221
catch (Exception ex)
132222
{
133-
exView.HandleException(ex);
223+
(new ExceptionView()).HandleException(ex);
134224
}
135225
}
136226

227+
////////////////////////////////////////////////////////////////
228+
/// Non-User Action Methods Region
229+
///
230+
/// This region contains methods that do not handle user actions.
231+
///
232+
/// Think about this is the back-end section.
233+
/// It should not be in a seperated class, because it directly interacts with the UI elements.
234+
////////////////////////////////////////////////////////////////
235+
137236
/*
138-
* Non-button methods
237+
* Refresh method to update the information displayed on the UI
139238
*/
140239
private async Task Refresh()
141240
{
241+
// Update the status bar to indicate loading
142242
StatusBarChange("Loading", true);
143243

244+
/*
245+
* Try to get info from the remote machine using WMI, with the target computer name
246+
* Catch the exception and display it using the ExceptionView if any error occurs
247+
* In any case, no matter if it succeeds or fails, update the status bar to "Done"
248+
*/
144249
try
145250
{
251+
// Connect to the WMI namespace
146252
machine.Connect("root\\cimv2");
147253

254+
// Get BIOS properties
255+
// In case the property is null, set the text to an empty string
148256
Task<ManagementObjectCollection> biosProperties = machine.GetObjects("Win32_BIOS", "Manufacturer, Name, SerialNumber, Version");
149257
foreach (ManagementObject biosProperty in (await biosProperties).Cast<ManagementObject>())
150258
{
@@ -154,6 +262,8 @@ private async Task Refresh()
154262
BIOS_Version.Text = biosProperty["Version"]?.ToString() ?? string.Empty;
155263
}
156264

265+
// Get Operating System properties
266+
// In case the property is null, set the text to an empty string
157267
Task<ManagementObjectCollection> osProperties = machine.GetObjects("Win32_ComputerSystem", "Name, Domain, TotalPhysicalMemory, SystemType");
158268
foreach (ManagementObject osProperty in (await osProperties).Cast<ManagementObject>())
159269
{
@@ -165,14 +275,19 @@ private async Task Refresh()
165275
}
166276
catch (Exception ex)
167277
{
168-
exView.HandleException(ex);
278+
// Handle any exceptions using the ExceptionView
279+
(new ExceptionView()).HandleException(ex);
169280
}
170281
finally
171282
{
283+
// Update the status bar to indicate completion
172284
StatusBarChange("Done", false);
173285
}
174286
}
175287

288+
/*
289+
* Set the status bar text and progress bar state
290+
*/
176291
protected void StatusBarChange(string label, bool progressbarLoading)
177292
{
178293
Bottom_Label.Text = label;

0 commit comments

Comments
 (0)