public void DoWorkAsync(Action<Object> workAction, IArgumentProvider provider) { exitState = ExitState.NORMAL; CancellationToken token = tokenSource.Token; Task mainTask = Task.Factory.StartNew(() => { while (true) { if (token.IsCancellationRequested) break; try { Object[] args = provider.GetArguments(); if (args.Length == 0) break; List<Task> tasks = new List<Task>(); foreach (Object arg in args) { if (token.IsCancellationRequested) break; try { Task subtask = Task.Factory.StartNew(workAction, arg, token); subtask.ContinueWith(TaskFailed, TaskContinuationOptions.OnlyOnFaulted); tasks.Add(subtask); } catch { exitState = ExitState.ERROR; tokenSource.Cancel(); break; } } try { Task.WaitAll(tasks.ToArray(), tokenSource.Token); } catch (OperationCanceledException) { } } catch { exitState = ExitState.ERROR; break; } } }, TaskCreationOptions.LongRunning); mainTask.ContinueWith(Done, scheduler); }