-2

This is my code :

  class Program
{
    static void Main(string[] args)
    {
        update();
    }

    static async void update()
    {
        await Task.Delay(100);
        Console.WriteLine("X");
        update();
    }
}

Console never outputs any text at all, and I have no clue why. What am I doing wrong?

2

1 Answer 1

1

Your Main method is not async, so it doesn't wait for your update method. Also, your update method should return a Task so your Main method can await it.

static async Task Main(string[] args)
{
    await update();
}

static async Task update()
{
    await Task.Delay(100);
    Console.WriteLine("X");
    await update();
}
2
  • @Selvin Thanks I'll update the answer
    – crgolden
    Commented Dec 15, 2020 at 10:25
  • Would that not cause an stack overflow?
    – peni4142
    Commented Apr 22, 2022 at 13:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.