comparison Implab/Components/RunnableComponent.cs @ 208:7d07503621fe v2

RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool) IRunnable is now disposable Code cleanups, suppressed some CodeAnalysis warnings
author cin
date Sun, 13 Nov 2016 18:28:17 +0300
parents 8200ab154c8a
children 5dc21f6a3222
comparison
equal deleted inserted replaced
207:558f34b2fb50 208:7d07503621fe
1 using System; 1 using System;
2 using System.Diagnostics.CodeAnalysis;
2 3
3 namespace Implab.Components { 4 namespace Implab.Components {
4 public abstract class RunnableComponent : IDisposable, IRunnable, IInitializable { 5 public abstract class RunnableComponent : IDisposable, IRunnable, IInitializable {
5 enum Commands { 6 enum Commands {
6 Ok = 0, 7 Ok = 0,
331 /// In normal cases the <see cref="Dispose()"/> method shouldn't be called, the call to the <see cref="Stop()"/> 332 /// In normal cases the <see cref="Dispose()"/> method shouldn't be called, the call to the <see cref="Stop()"/>
332 /// method is sufficient to cleanup the component. Call <see cref="Dispose()"/> only to cleanup after errors, 333 /// method is sufficient to cleanup the component. Call <see cref="Dispose()"/> only to cleanup after errors,
333 /// especially if <see cref="Stop"/> method is failed. Using this method insted of <see cref="Stop()"/> may 334 /// especially if <see cref="Stop"/> method is failed. Using this method insted of <see cref="Stop()"/> may
334 /// lead to the data loss by the component. 335 /// lead to the data loss by the component.
335 /// </para></remarks> 336 /// </para></remarks>
337 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
336 public void Dispose() { 338 public void Dispose() {
337 IPromise pending; 339 IPromise pending;
338 340
339 lock (m_stateMachine) { 341 lock (m_stateMachine) {
340 if (m_stateMachine.State == ExecutionState.Disposed) 342 if (m_stateMachine.State == ExecutionState.Disposed)
341 return; 343 return;
342 pending = Move(Commands.Dispose, null, null); 344 Move(Commands.Dispose, null, null);
343 } 345 }
344 346
345 GC.SuppressFinalize(this); 347 GC.SuppressFinalize(this);
346 if (pending != null) { 348 Dispose(true);
347 pending.Cancel();
348 pending.Timeout(DisposeTimeout).On(
349 () => Dispose(true, null),
350 err => Dispose(true, err),
351 reason => Dispose(true, new OperationCanceledException("The operation is cancelled", reason))
352 );
353 } else {
354 Dispose(true, null);
355 }
356 } 349 }
357 350
358 ~RunnableComponent() { 351 ~RunnableComponent() {
359 Dispose(false, null); 352 Dispose(false);
360 } 353 }
361 354
362 #endregion 355 #endregion
363 356
364 /// <summary> 357 /// <summary>
365 /// Releases all resources used by the component, called automatically, override this method to implement your cleanup. 358 /// Releases all resources used by the component, called automatically, override this method to implement your cleanup.
366 /// </summary> 359 /// </summary>
367 /// <param name="disposing">true if this method is called during normal dispose process.</param> 360 /// <param name="disposing">true if this method is called during normal dispose process.</param>
368 /// <param name="lastError">The last error which occured during the component stop.</param> 361 /// <param name="pending">The operation which is currenty pending</param>
369 protected virtual void Dispose(bool disposing, Exception lastError) { 362 protected virtual void Dispose(bool disposing) {
370 363
371 } 364 }
372 365
373 } 366 }
374 } 367 }