1
- using System . Diagnostics ;
1
+ using System . Diagnostics ;
2
+
3
+ const string NO_COLOR = "--no-color" ;
4
+ const string INTERVAL = "-i" ;
5
+ const string HELP = "-h" ;
2
6
3
7
bool running = true ;
4
8
bool isColorOutput = true ;
5
9
bool isIntervalMode = false ;
6
- Stopwatch stopWatch = new Stopwatch ( ) ;
7
10
int interval = 1500 ;
11
+ int paramIndex = 0 ;
12
+ var usedParameters = new List < string > ( ) ;
13
+ var availableParameters = new [ ] { new Parameter { parameter = HELP , execution = HelpParameter } ,
14
+ new Parameter { parameter = INTERVAL , execution = IntervalParameter } ,
15
+ new Parameter { parameter = NO_COLOR , execution = NoColorParameter } } ;
8
16
9
17
void StopRunning ( object ? sender , ConsoleCancelEventArgs args ) => running = false ;
10
18
11
19
Console . CancelKeyPress += StopRunning ;
12
20
13
- if ( args . Length > 0 && args . Contains ( "--no-color" ) )
14
- {
15
- isColorOutput = false ;
16
- args = args . Except ( new [ ] { "--no-color" } ) . ToArray ( ) ;
17
- }
21
+ ProcessParameters ( ) ;
18
22
19
- if ( args . Length > 0 && args . Contains ( "-i" ) )
23
+ if ( isIntervalMode )
20
24
{
21
- var temp = string . Empty ;
22
- for ( int i = 0 ; i < args . Length ; i ++ )
23
- if ( args [ i ] == "-i" )
24
- {
25
- try
26
- {
27
- temp = args [ i + 1 ] ;
28
- interval = Convert . ToInt32 ( temp ) ;
29
- break ;
30
- }
31
- catch
32
- {
33
- return ;
34
- }
35
- }
36
- isIntervalMode = true ;
37
- args = args . Except ( new [ ] { "-i" , temp } ) . ToArray ( ) ;
25
+ Stopwatch stopWatch = new Stopwatch ( ) ;
26
+ stopWatch . Start ( ) ;
27
+ do
28
+ {
29
+ if ( stopWatch . ElapsedMilliseconds < interval ) continue ;
30
+ stopWatch . Restart ( ) ;
31
+ MainAlgorithm ( ) ;
32
+ } while ( running ) ;
38
33
}
39
-
40
- stopWatch . Start ( ) ;
41
- do
42
- {
43
- if ( stopWatch . ElapsedMilliseconds < interval && isIntervalMode ) continue ;
44
- stopWatch . Restart ( ) ;
34
+ else
45
35
MainAlgorithm ( ) ;
46
- } while ( isIntervalMode ) ;
47
- stopWatch . Stop ( ) ;
48
36
49
37
void MainAlgorithm ( )
50
38
{
@@ -125,6 +113,62 @@ void MainAlgorithm()
125
113
}
126
114
}
127
115
}
116
+ void ProcessParameters ( )
117
+ {
118
+ if ( args . Length <= 0 ) return ;
119
+ for ( int i = 0 ; i < args . Length ; i ++ )
120
+ {
121
+ if ( args [ i ] [ 0 ] != '-' ) continue ;
122
+ foreach ( var param in availableParameters )
123
+ {
124
+ if ( param . parameter != args [ i ] ) continue ;
125
+ paramIndex = i ;
126
+ param . execution ( ) ;
127
+ paramIndex = 0 ;
128
+ break ;
129
+ }
130
+ }
131
+ args = args . Except ( usedParameters ) . ToArray ( ) ;
132
+ usedParameters . Clear ( ) ;
133
+ }
134
+
135
+ #region PARAMETERS
136
+ void NoColorParameter ( )
137
+ {
138
+ isColorOutput = false ;
139
+ usedParameters . Add ( NO_COLOR ) ;
140
+ }
141
+
142
+ void IntervalParameter ( )
143
+ {
144
+ isIntervalMode = true ;
145
+ try
146
+ {
147
+ var temp = args [ paramIndex + 1 ] ;
148
+ interval = Convert . ToInt32 ( temp ) ;
149
+ usedParameters . AddRange ( new [ ] { args [ paramIndex ] , temp } ) ;
150
+ }
151
+ catch
152
+ {
153
+ usedParameters . Add ( args [ paramIndex ] ) ;
154
+ }
155
+ }
156
+
157
+ void HelpParameter ( )
158
+ {
159
+ running = false ;
160
+ try
161
+ {
162
+ var temp = args [ paramIndex + 1 ] ;
163
+ //Do something with temp.
164
+ usedParameters . AddRange ( new [ ] { args [ paramIndex ] , temp } ) ;
165
+ }
166
+ catch
167
+ {
168
+ usedParameters . Add ( args [ paramIndex ] ) ;
169
+ }
170
+ }
171
+ #endregion PARAMETERS
128
172
129
173
static void WriteLineColor ( string content , ConsoleColor color , params ConsoleColor [ ] background )
130
174
{
@@ -145,7 +189,7 @@ static string FindIndexedProcessName(int pId)
145
189
for ( var i = 0 ; i < processesByName . Length ; i ++ )
146
190
{
147
191
processIdName = i == 0 ? processName : $ "{ processName } #{ i } ";
148
- var processId = new PerformanceCounter ( "Process" , "ID Process" , processIdName ) ;
192
+ using var processId = new PerformanceCounter ( "Process" , "ID Process" , processIdName ) ;
149
193
if ( ( int ) processId . NextValue ( ) == pId ) return processIdName ;
150
194
}
151
195
@@ -156,20 +200,19 @@ static string FindIndexedProcessName(int pId)
156
200
return string . Empty ;
157
201
}
158
202
}
159
-
160
- static Process ? FindPidFromIndexedProcessName ( string indexedProcessName )
203
+ static Process ? FindCreatingProcessID ( string indexedProcessName )
161
204
{
162
205
try
163
206
{
164
- return Process . GetProcessById ( ( int ) new PerformanceCounter ( "Process" , "Creating Process ID" , indexedProcessName ) . NextValue ( ) ) ;
207
+ using var processParent = new PerformanceCounter ( "Process" , "Creating Process ID" , indexedProcessName ) ;
208
+ return Process . GetProcessById ( ( int ) processParent . NextValue ( ) ) ;
165
209
}
166
210
catch
167
211
{
168
212
return null ;
169
213
}
170
214
}
171
-
172
- static Process ? GetParent ( Process process ) => FindPidFromIndexedProcessName ( FindIndexedProcessName ( process . Id ) ) ;
215
+ static Process ? GetParent ( Process process ) => FindCreatingProcessID ( FindIndexedProcessName ( process . Id ) ) ;
173
216
174
217
static void DisplayProcessInfo ( string tab , Process p , Process ? parent )
175
218
{
@@ -179,12 +222,17 @@ static void DisplayProcessInfo(string tab, Process p, Process? parent)
179
222
Console . Write ( $ "{ tab } Module Name | ") ; WriteLineColor ( $ "{ p . MainModule ? . ModuleName } ", ConsoleColor . DarkMagenta ) ;
180
223
Console . Write ( $ "{ tab } Module Path | ") ; WriteLineColor ( $ "{ p . MainModule ? . FileName . Replace ( p . MainModule ? . ModuleName ?? string . Empty , string . Empty ) } ", ConsoleColor . DarkMagenta ) ;
181
224
}
182
-
183
225
static void DisplayProcessInfoWhite ( string tab , Process p , Process ? parent )
184
226
{
185
- Console . WriteLine ( $ "{ tab } Parent PID | { parent ? . Id } ") ;
186
- Console . WriteLine ( $ "{ tab } Name | { p . ProcessName } ") ;
187
- Console . WriteLine ( $ "{ tab } Working Set (x64) | { ( float ) ( p . WorkingSet64 * 0.0000001 ) } mb") ;
188
- Console . WriteLine ( $ "{ tab } Module Name | { p . MainModule ? . ModuleName } ") ;
189
- Console . WriteLine ( $ "{ tab } Module Path | { p . MainModule ? . FileName . Replace ( p . MainModule ? . ModuleName ?? string . Empty , string . Empty ) } ") ;
227
+ Console . Write ( $ "{ tab } Parent PID | ") ; Console . WriteLine ( $ "{ parent ? . Id } ") ;
228
+ Console . Write ( $ "{ tab } Name | ") ; Console . WriteLine ( $ "{ p . ProcessName } ") ;
229
+ Console . Write ( $ "{ tab } Working Set (x64) | ") ; Console . WriteLine ( $ "{ ( float ) ( p . WorkingSet64 * 0.0000001 ) } mb") ;
230
+ Console . Write ( $ "{ tab } Module Name | ") ; Console . WriteLine ( $ "{ p . MainModule ? . ModuleName } ") ;
231
+ Console . Write ( $ "{ tab } Module Path | ") ; Console . WriteLine ( $ "{ p . MainModule ? . FileName . Replace ( p . MainModule ? . ModuleName ?? string . Empty , string . Empty ) } ") ;
232
+ }
233
+
234
+ struct Parameter
235
+ {
236
+ public string parameter ;
237
+ public Action execution ;
190
238
}
0 commit comments