What is the correct way to use async/await in a recursive method?

后端 未结 2 1777
太阳男子
太阳男子 2020-12-06 18:43

What is the correct way to use async/await in a recursive method? Here is my method:

public string ProcessStream(string streamPosition)
{
    var stream = Ge         


        
2条回答
  •  独厮守ぢ
    2020-12-06 18:56

    While I have to say upfront that the intention of the method is not entirely clear to me, reimplementing it with a simple loop is quite trivial:

    public async Task ProcessStream(string streamPosition)
    {
        while (true)
        {
            var stream = GetStream(streamPosition);
    
            if (stream.Items.Count == 0)
                return stream.NextPosition;
    
            foreach (var item in stream.Items)
            {
                await ProcessItem(item); //ProcessItem() is now an async method
            }
    
            streamPosition = stream.NextPosition;
        }
    }
    

    Recursion is not stack-friendly and if you have the option of using a loop, it's something definitely worth looking into in simple synchronous scenarios (where poorly controlled recursion eventually leads to StackOverflowExceptions), as well as asynchronous scenarios, where, I'll be honest, I don't even know what would happen if you push things too far (my VS Test Explorer crashes whenever I try to reproduce known stack overflow scenarios with async methods).

    Answers such as Recursion and the await / async Keywords suggest that StackOverflowException is less of a problem with async due to the way the async/await state machine works, but this is not something I have explored much as I tend to avoid recursion whenever possible.

提交回复
热议问题