简体   繁体   中英

Async wait for multiple threads to finish

I have a code block which is eventually accessed by multiple threads. I search for an up to date async mechanism to continue executing when all threads have passed.

Currently I do the following with a CountDownEvent which works just fine (without async support).

    public class Watcher
{
    private static readonly Logger Log = LogManager.GetCurrentClassLogger();

    private readonly CountdownEvent _isUpdating = new CountdownEvent(1);
    private readonly IActivity _activity;

    public Watcher([NotNull] IActivity activity)
    {
        _activity = activity ?? throw new ArgumentNullException(nameof(activity));
        _activity.Received += OnReceived;
    }

    private void OnReceived(IReadOnlyCollection<Summary> summaries)
    {
        _isUpdating.AddCount();

        try
        {
            // Threads processing
        }
        finally
        {
            _isUpdating.Signal();
        }
    }

    private void Disable()
    {
        _activity.Received -= OnReceived;

        _isUpdating.Signal();

        /* await */ _isUpdating.Wait();
    }
}

Do I need to use any of those AsyncCountdownEvent implementations or is there any other built-in mechanism? I already thought about using a BufferBlock because it has async functionality but I think it's a bit overkill.

Additional to the comments:

IActivity is a WebService call (but shouldn't effect the implementation on top or vice versa)

        public async Task Start(bool alwayRetry = true, CancellationToken cancellationToken = new CancellationToken())
    {
        var milliseconds = ReloadSeconds * 1000;

        do
        {
            try
            {
                var summaries = await PublicAPI.GetSummariesAsync(cancellationToken).ConfigureAwait(false);
                OnSummariesReceived(summaries);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);

                OnErrorOccurred(ex);
            }

            await Task.Delay(milliseconds, cancellationToken).ConfigureAwait(false);
            // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
        } while (alwayRetry);
    }

It's not clear the IActivity signatures; but you can wait for a range of tasks to be completed:

class MultiAsyncTest {

    Task SomeAsync1() { return Task.Delay(1000); }

    Task SomeAsync2() { return Task.Delay(2000);}

    Task EntryPointAsync() {
        var tasks = new List<Task>();
        tasks.Add(SomeAsync1());
        tasks.Add(SomeAsync2());
        return Task.WhenAll(tasks);
    }

}

What's IActivity 's signature? Does it support Task ? Or you are using Thread ? More explanation would help to a more specified answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM