@@ -119,33 +119,49 @@ await Task.Run(() =>
119
119
120
120
private async void WatchProcess ( ProcessResult process , CancellationToken token )
121
121
{
122
- Process handle = Process . GetProcessById ( ( int ) process . pid ) ;
123
122
try
124
123
{
125
- await handle . WaitForExitAsync ( token ) ;
126
- }
127
- catch ( TaskCanceledException )
128
- {
129
- // Nothing to do, normal operation
130
- }
124
+ Process handle = Process . GetProcessById ( ( int ) process . pid ) ;
125
+ try
126
+ {
127
+ await handle . WaitForExitAsync ( token ) ;
128
+ }
129
+ catch ( TaskCanceledException )
130
+ {
131
+ // Nothing to do, normal operation
132
+ }
131
133
132
- if ( handle . HasExited )
134
+ if ( handle . HasExited )
135
+ {
136
+ Processes . Remove ( process ) ;
137
+ }
138
+ }
139
+ catch ( Exception ex )
133
140
{
134
- Processes . Remove ( process ) ;
141
+ Logger . LogError ( $ "Couldn't add a waiter to wait for a process to exit. PID = { process . pid } and Name = { process . name } .", ex ) ;
142
+ Processes . Remove ( process ) ; // If we couldn't get an handle to the process or it has exited in the meanwhile, don't show it.
135
143
}
136
144
}
137
145
138
146
[ RelayCommand ]
139
147
public void EndTask ( ProcessResult selectedProcess )
140
148
{
141
- Process handle = Process . GetProcessById ( ( int ) selectedProcess . pid ) ;
142
149
try
143
150
{
144
- handle . Kill ( ) ;
151
+ Process handle = Process . GetProcessById ( ( int ) selectedProcess . pid ) ;
152
+ try
153
+ {
154
+ handle . Kill ( ) ;
155
+ }
156
+ catch ( Exception ex )
157
+ {
158
+ Logger . LogError ( $ "Couldn't kill process { selectedProcess . name } with PID { selectedProcess . pid } .", ex ) ;
159
+ }
145
160
}
146
- catch ( Exception )
161
+ catch ( Exception ex )
147
162
{
148
- Logger . LogError ( $ "Couldn't kill process { selectedProcess . name } with PID { selectedProcess . pid } .") ;
163
+ Logger . LogError ( $ "Couldn't get an handle to kill process { selectedProcess . name } with PID { selectedProcess . pid } . Likely has been killed already.", ex ) ;
164
+ Processes . Remove ( selectedProcess ) ; // If we couldn't get an handle to the process, remove it from the list, since it's likely been killed already.
149
165
}
150
166
}
151
167
0 commit comments