Igby Largeman
Igby Largeman

Reputation: 16747

Is it possible to wait for unknown Tasks to complete?

Using the .NET Task Parallel Library, is there an in-built way to wait for all running Tasks to complete, without actually having a reference to the Tasks?

Task.WaitAny() and Task.WaitAll() both require an array of Task instances. What I'd like is something like Task.WaitAll() which takes no arguments, and just waits for any and all Tasks spawned by my application to complete.

If not, I'll build something into my framework, but I don't want to reinvent the wheel if I can help it.

Upvotes: 3

Views: 996

Answers (2)

Arne Claassen
Arne Claassen

Reputation: 14414

Since any Task could be spawned by any .NET code (i.e. BCL, some library, etc.) you probably don't want to wait on all tasks that are outstanding, but just the ones that your own code has created. The simplest way is to create your own factory that uses the underlying one that tracks all unfinished tasks and exposes the desired wait functions

public class MyTaskFactory {
    private HashSet<Task> _tasks = new HashSet<Task>();

    public Task<T> StartNew<T>(Func<T> func) {
        var t = Task.Factory.StartNew(func);
        t.ContinueWith(x => {
          lock(_tasks) {
            _tasks.Remove(x);
          }
        });
        lock(_tasks) {
          _tasks.Add(t);
        }
        return t;
    }

    // ... implement other StartNew overrides ...

    public void WaitAll() {
        Task[] tasks;
        lock(_tasks) {
          tasks = _tasks.ToArray();
        }
        Task.WaitAll(tasks);
    }
}

Upvotes: 4

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52137

I know this doesn't answer your question directly, but you could change your architecture so all the tasks are created as children of one "main" task. Waiting on this "main" task will then automatically wait on all its children.

You can create a child task like this:

Task.Factory.StartNew(
    () => /* ... */,
    TaskCreationOptions.AttachedToParent
);

You may find the following links useful when it comes to the topic of child tasks:

Upvotes: 5

Related Questions