-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?

Haikuno
  • 3
  • 1
  • 2
    Does this answer your question? [async/await - when to return a Task vs void?](https://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void) – IndieGameDev Dec 15 '20 at 10:12
  • 1
    [Avoid async void](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void). Use `async Task` instead. – Theodor Zoulias Dec 15 '20 at 10:19

1 Answers1

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();
}
crgolden
  • 4,332
  • 1
  • 22
  • 40