@@ -160,19 +160,27 @@ protected virtual string CommandLineArguments
160
160
/// <summary>
161
161
/// Gets a value indicating the time to wait for an initial connection before timing out.
162
162
/// </summary>
163
- protected virtual TimeSpan InitialConnectionTimeout
163
+ protected virtual TimeSpan InitializationTimeout
164
164
{
165
165
get { return TimeSpan . FromSeconds ( 20 ) ; }
166
166
}
167
167
168
+ /// <summary>
169
+ /// Gets a value indicating the time to wait for the service to terminate before forcing it to terminate.
170
+ /// </summary>
171
+ protected virtual TimeSpan TerminationTimeout
172
+ {
173
+ get { return TimeSpan . FromSeconds ( 10 ) ; }
174
+ }
175
+
168
176
/// <summary>
169
177
/// Gets a value indicating whether the service is responding to HTTP requests.
170
178
/// </summary>
171
- protected virtual bool IsAvailable
179
+ protected virtual bool IsInitialized
172
180
{
173
181
get
174
182
{
175
- bool isAvailable = false ;
183
+ bool isInitialized = false ;
176
184
try
177
185
{
178
186
Uri serviceHealthUri = new Uri ( this . ServiceUrl , new Uri ( DriverCommand . Status , UriKind . Relative ) ) ;
@@ -185,15 +193,15 @@ protected virtual bool IsAvailable
185
193
// that the HTTP status returned is a 200 status, and that the resposne has the correct
186
194
// Content-Type header. A more sophisticated check would parse the JSON response and
187
195
// validate its values. At the moment we do not do this more sophisticated check.
188
- isAvailable = response . StatusCode == HttpStatusCode . OK && response . ContentType . StartsWith ( "application/json" , StringComparison . OrdinalIgnoreCase ) ;
196
+ isInitialized = response . StatusCode == HttpStatusCode . OK && response . ContentType . StartsWith ( "application/json" , StringComparison . OrdinalIgnoreCase ) ;
189
197
response . Close ( ) ;
190
198
}
191
199
catch ( WebException ex )
192
200
{
193
201
Console . WriteLine ( ex . Message ) ;
194
202
}
195
203
196
- return isAvailable ;
204
+ return isInitialized ;
197
205
}
198
206
}
199
207
@@ -218,7 +226,7 @@ public void Start()
218
226
this . driverServiceProcess . StartInfo . UseShellExecute = false ;
219
227
this . driverServiceProcess . StartInfo . CreateNoWindow = this . hideCommandPromptWindow ;
220
228
this . driverServiceProcess . Start ( ) ;
221
- bool serviceAvailable = WaitForServiceAvailable ( ) ;
229
+ bool serviceAvailable = WaitForServiceInitialization ( ) ;
222
230
223
231
if ( ! serviceAvailable )
224
232
{
@@ -265,12 +273,11 @@ protected virtual void Dispose(bool disposing)
265
273
[ SecurityPermission ( SecurityAction . Demand ) ]
266
274
private void Stop ( )
267
275
{
268
- if ( this . driverServiceProcess != null && ! this . driverServiceProcess . HasExited )
276
+ if ( this . IsRunning )
269
277
{
270
278
Uri shutdownUrl = new Uri ( this . ServiceUrl , "/shutdown" ) ;
271
- DateTime timeout = DateTime . Now . Add ( TimeSpan . FromSeconds ( 10 ) ) ;
272
- bool processStopped = false ;
273
- while ( ! processStopped && DateTime . Now < timeout )
279
+ DateTime timeout = DateTime . Now . Add ( this . TerminationTimeout ) ;
280
+ while ( this . IsRunning && DateTime . Now < timeout )
274
281
{
275
282
try
276
283
{
@@ -284,20 +291,18 @@ private void Stop()
284
291
HttpWebResponse response = request . GetResponse ( ) as HttpWebResponse ;
285
292
response . Close ( ) ;
286
293
this . driverServiceProcess . WaitForExit ( 3000 ) ;
287
- processStopped = this . driverServiceProcess . HasExited ;
288
294
}
289
295
catch ( WebException )
290
296
{
291
- processStopped = true ;
292
297
}
293
298
}
294
299
295
300
// If at this point, the process still hasn't exited, wait for one
296
301
// last-ditch time, then, if it still hasn't exited, kill it. Note
297
302
// that falling into this branch of code should be exceedingly rare.
298
- if ( ! this . driverServiceProcess . HasExited )
303
+ if ( this . IsRunning )
299
304
{
300
- this . driverServiceProcess . WaitForExit ( 5000 ) ;
305
+ this . driverServiceProcess . WaitForExit ( Convert . ToInt32 ( this . TerminationTimeout . TotalMilliseconds ) ) ;
301
306
if ( ! this . driverServiceProcess . HasExited )
302
307
{
303
308
this . driverServiceProcess . Kill ( ) ;
@@ -310,27 +315,27 @@ private void Stop()
310
315
}
311
316
312
317
/// <summary>
313
- /// Waits until a the service is available , or the timeout set
314
- /// by the <see cref="InitialConnectionTimeout "/> property is reached.
318
+ /// Waits until a the service is initialized , or the timeout set
319
+ /// by the <see cref="InitializationTimeout "/> property is reached.
315
320
/// </summary>
316
321
/// <returns><see langword="true"/> if the service is properly started and receiving HTTP requests;
317
322
/// otherwise; <see langword="false"/>.</returns>
318
- private bool WaitForServiceAvailable ( )
323
+ private bool WaitForServiceInitialization ( )
319
324
{
320
- bool isAvailable = false ;
321
- DateTime timeout = DateTime . Now . Add ( this . InitialConnectionTimeout ) ;
322
- while ( ! isAvailable && DateTime . Now < timeout )
325
+ bool isInitialized = false ;
326
+ DateTime timeout = DateTime . Now . Add ( this . InitializationTimeout ) ;
327
+ while ( ! isInitialized && DateTime . Now < timeout )
323
328
{
324
329
// If the driver service process has exited, we can exit early.
325
330
if ( ! this . IsRunning )
326
331
{
327
332
break ;
328
333
}
329
334
330
- isAvailable = this . IsAvailable ;
335
+ isInitialized = this . IsInitialized ;
331
336
}
332
337
333
- return isAvailable ;
338
+ return isInitialized ;
334
339
}
335
340
}
336
341
}
0 commit comments