Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous iterator Task<IEnumerable<T>>

I’m trying to implement an asynchronous function that returns an iterator. The idea is the following:

    private async Task<IEnumerable<char>> TestAsync(string testString)     {         foreach (char c in testString.ToCharArray())         {             // do other work             yield return c;         }     } 

However, there is an error message that the function cannot be an iterator block because Task<IEnumerable<char>> is not an iterator interface type. Is there a solution?

like image 876
user2341923 Avatar asked Apr 25 '14 13:04

user2341923


People also ask

What is task IEnumerable?

WhenAll(IEnumerable<Task>) Creates a task that will complete when all of the Task objects in an enumerable collection have completed. WhenAll(Task[]) Creates a task that will complete when all of the Task objects in an array have completed.

What is task in asynchronous?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What is Asyncenumerable?

Async Streams or IAsyncEnumerable<T> provides a way to iterate over an IEnumerable collection asynchronously while using the yield operator to return data as it comes in.

Is ForEach async in C#?

ForEach doesn't play particularly well with async (neither does LINQ-to-objects, for the same reasons). In this case, I recommend projecting each element into an asynchronous operation, and you can then (asynchronously) wait for them all to complete.


1 Answers

It sounds like what you may really be looking for is something like IObservable<T>, which is sort of like a push-based asynchronous IEnumerable<T>. See Reactive Extensions, a.k.a. Rx (code licensed under MIT) (no affiliation) for a huge host of methods that work with IObservable<T> to make it work like LINQ-to-Objects and more.

The problem with IEnumerable<T> is that there's nothing that really makes the enumeration itself asynchronous. If you don't want to add a dependency on Rx (which is really what makes IObservable<T> shine), this alternative might work for you:

public async Task<IEnumerable<char>> TestAsync(string testString) {     return GetChars(testString); }  private static IEnumerable<char> GetChars(string testString) {     foreach (char c in testString.ToCharArray())     {         // do other work         yield return c;     } } 

though I'd like to point out that without knowing what's actually being done asynchronously, there may be a much better way to accomplish your goals. None of the code you posted will actually do anything asynchronously, and I don't really know if anything in // do other work is asynchronous (in which case, this isn't a solution to your underlying problem though it will make your code compile).

like image 167
Joe Amenta Avatar answered Sep 29 '22 18:09

Joe Amenta