A simple C# library to enable you to wait for multiple tasks to complete.
ParallelCompletion used two new C# 9 features in code:
new()
constructor callsTaskCompletionSource
without a return type
You can manually mitigate them and downgrade to C# 8 or earlier in order to use this library.
Note: Each instance of ParallelTokenSource
can only be used once. After WaitForCompletion()
calls, they were invalidated and calling again will result in InvalidOperationException
.
using ParallelCompletion;
...
// setup
var pts = new ParallelTokenSource();
var token = pts.CreateToken();
// run tasks
_ = Task.Run(async () =>
{
...
token.Complete();
});
// wait for completion
await pts.WaitForCompletion();
using ParallelCompletion;
...
// setup
var pts = new ParallelTokenSource();
pts.RunTask( ... );
// wait for completion
await pts.WaitForCompletion();
using ParallelCompletion;
using ParallelCompletion.Extensions;
...
// setup
var pts = new ParallelTokenSource();
...
someTask.RegisterTo(pts);
// wait for completion
await pts.WaitForCompletion();