C#. Async / Await. Simple example

This is a simple example, we don’t wait for DoSmthAsync() in our calling main thread, just going further, when it is done, using its result in calling thread.

    class Program
    {
        static void Main(string[] args)
        {
            DoSmthAsync();
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(1000);
                Console.WriteLine(i);
            }
        }

        static async Task DoSmthAsync()
        {
            Console.WriteLine("startedWait");
            await Task.Delay(2000);
            Console.WriteLine("finishedWait");
        }
    }

output

This entry was posted in Без рубрики. Bookmark the permalink.