0

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);
    }
4

1 回答 1

0

IActivity签名不清楚;但您可以等待一系列任务完成:

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);
    }

}

什么IActivity是签名?它支持Task吗?或者你正在使用Thread?更多解释将有助于获得更具体的答案。

于 2018-02-21T12:24:51.493 回答