1

I want to print every 2 sec number but and in the end i get only 0 what i need to do to get this every 2 sec?

result:

    0
    1
    .
    .
    49

  private static void Main(string[] args) {
    Send();
  }

  public static async Task Send() {
    for (int i = 0; i < 50; i++) {
        Console.WriteLine(i);
        await Task.Delay(2000);
    }
  }
maxteneff
  • 1,523
  • 12
  • 28
gal
  • 49
  • 4

3 Answers3

1

well, simply because your Main method won't wait for the send method to finish and you can't use the await keyword in order for that to happened since the compiler won't allow an async Main, in that case you could simple use a Task.Run

 private static void Main(string[] args)
    {
        Task.Run(async () =>
        {
            await Send();
        }).Wait();
    }

    public static async Task Send()
    {
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine(i);
            await Task.Delay(2000);
        }
    }
  • if i add Console.WriteLine(99); after the task in main its printing just in the and so why **send** stack like regular func? – gal Dec 19 '15 at 23:08
  • i didn't get what you are asking for ?, you wan't to print 99 once the loop is over ? if that is what you were asking for so adding Console.WriteLine(99); after the Task.Run(.. block will do the trick otherwise you could always use the .ContinueWith(.. after the Task.Run ! –  Dec 19 '15 at 23:12
  • This is a good solution. I've provided a more general "How to do asynchronous Console apps" answer below that builds on this one. – Shazi Dec 20 '15 at 09:53
0

What you are missing is a Console.ReadLine() call, to halt the console from exiting the program before your task gets executed. Tested the code below and it works.

    private static void Main(string[] args)
    {
        Send();
        Console.ReadLine();
    }

    public static async Task Send()
    {
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine(i);
            await Task.Delay(2000);
        }
    }
Toni Kostelac
  • 351
  • 3
  • 17
  • Actually this is not a good solution. Since you do not wait on the task returning from the call to "Send" then your "Console.ReadLine()" could (and probably would") be executed Before Send() actually finishes which is probably not what anyone wants... – Shazi Dec 20 '15 at 09:37
0

Building on Josephs answer, but in a more general "How to do asynchronous console apps"-way

Using asynchronous code in console apps can be a bit tricky. I generally use a substitute MainAsync method that the original Main() calls and makes a blocking wait on.

Here comes an example of how it could be done:

class Program
{
    static void Main(string[] args)
    {
        Task mainTask = MainAsync(args);
        mainTask.Wait();
        // Instead of writing more code here, use the MainAsync-method as your new Main()
    }

    static async Task MainAsync(string[] args)
    {
        await Send();
        Console.ReadLine();
    }

    public static async Task Send()
    {
        for (int i = 0; i < 50; i++)
        {
            Console.WriteLine(i);
            await Task.Delay(2000);
        }
    }
}
Shazi
  • 1,490
  • 1
  • 10
  • 22