AsyncEx icon indicating copy to clipboard operation
AsyncEx copied to clipboard

Add IsCompleted to AsyncCollection

Open Denis535 opened this issue 5 years ago • 4 comments

BlockingCollection has property IsCompleted. But your AsyncCollection hasn't public IsCompleted. Can you add it? I need it to check producer is completed or not.

Denis535 avatar May 22 '19 13:05 Denis535

In my experience, adding properties like this tends to encourage writing code that has race conditions. Can you explain in more detail why you need to check whether the producer has completed, and why OutputAvailableAsync wouldn't work?

StephenCleary avatar May 22 '19 13:05 StephenCleary

I have functions like this:

// Task is completed when stream is completed or canceled
Task GetStreamAsync(Action<object> onItem, CancellationToken cancellationToken = default);

Then I transform this function into AsyncCollection:

try {
    await exchange.GetStreamAsync( collection.Add, cancellationToken );
} finally {
    collection.CompleteAdding();
}

Then I transform AsyncCollection into IAsyncEnumerable:

        public static async IAsyncEnumerable<T> GetConsumingAsyncEnumerable<T>(this AsyncCollection<T> collection) {
            while (await collection.OutputAvailableAsync()) {
                yield return collection.Take();
            }
        }

Then I need to determine either IAsyncEnumerable was finished or breaked. If breaked I should close connection. So, I can check Task.IsCompleted or AsyncCollection.IsCompleted. I wanted second, but you haven't this property.

Denis535 avatar May 22 '19 13:05 Denis535

Stephen, I came here looking for whether GetConsumingAsyncEnumerable. Does it make sense to have GetConsumingAsyncEnumerable available for AsyncCollection?

jaredthirsk avatar Nov 02 '19 14:11 jaredthirsk

@jaredthirsk Yes, I'm planning to add a ReadAllAsync method that allows consuming as an async stream.

StephenCleary avatar Dec 12 '19 00:12 StephenCleary