public virtual ApiResult Shutdown() { ThrowIfNotOk(); shutdownRequested = true; if (!cancellationToken.IsCancellationRequested) { Global.CancellationTokenSource.Cancel(); } int waitCount = 0; while (Threads.Any(t => t.IsAlive) && waitCount < 30) { Thread.Sleep(100); waitCount++; } if (Threads.All(t => !t.IsAlive)) { shutdownCompleted = true; Info("All threads stopped. {0} service shutdown completed successfully.", Name); return(ApiResult.Success); } else { Info("{0} threads in {1} did not stop. Aborting {0} threads", Threads.Count(t => t.IsAlive), this.GetType().Name); foreach (Thread thread in Threads.Where(t => t.IsAlive)) { thread.Abort(); } if (Threads.All(t => !t.IsAlive)) { shutdownCompleted = true; Info("All threads stopped. {0} service shutdown completed successfully.", Name); return(ApiResult.Success); } else { return(ApiResult.Failure); } } }