AsyncEx
AsyncEx copied to clipboard
Add IsCompleted to AsyncCollection
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.
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?
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.
Stephen, I came here looking for whether GetConsumingAsyncEnumerable. Does it make sense to have GetConsumingAsyncEnumerable available for AsyncCollection?
@jaredthirsk Yes, I'm planning to add a ReadAllAsync
method that allows consuming as an async stream.