public Task ShutdownAsync() { if (_timer != null) { // .NET-specific ExecutorService.Cancel(_timer); } var tcsShutdown = new TaskCompletionSource <object>(); lock (_lock) { _shutdown = true; int max = _runningTasks.Count; if (max == 0) { tcsShutdown.SetResult(null); return(tcsShutdown.Task); } var counter = new VolatileInteger(0); foreach (var task in _runningTasks.Keys) { task.ContinueWith(t => { if (counter.IncrementAndGet() == max) { tcsShutdown.SetResult(null); // complete } }); } } return(tcsShutdown.Task); }
private void Destroy() { _state = 2; // .NET-specific: if (_timer != null) { ExecutorService.Cancel(_timer); _timer = null; } if (_executor != null) { _executor.Shutdown(); } }