Benefits of using async and keyword expectations

I am new to using asynchronous methods in C #. I read that these keywords asyncalso awaithelp to make the program more susceptible, asynchronizing some methods. I have this snippet:

First way

    public static void Main()
    {
        Console.WriteLine("Hello!! welcome to task application");
        Console.ReadKey();
        Task<string> ourtask = Task.Factory.StartNew<string>(() =>
        {
            return "Good Job";
        });
        ourtask.Wait();
        Console.WriteLine(ourtask.Result);
        Console.ReadKey();
    }

Second way

 public static void Main()
        {
            Launch();
        }
        public static async void Launch()
        {
            Console.WriteLine("Hello!! welcome to task application");
            Console.ReadKey();
            Console.WriteLine(await GetMessage());
            Console.ReadKey();
        }

        public static Task<string> GetMessage()
        {
            return Task.Factory.StartNew<string>(() =>
                {
                    return "Good Job";
                });
        }

I need to know:

  • Is there a difference between the two implementations (in the concept of parallelism)?

  • What are the benefits of using keywords asyncand awaitif I can just create a task and wait for it to complete?

+4
source share
3 answers

, . , , , .

, Volkswagen Beetle, , , 24- . , , , , , .

, . , , !

, ? . , - , , .

? : .

Task, . , .Wait(), .

await, .

+33

:

  • () parallelism. : 1) , , , 2) . await, Main(), , Launch(), .

  • async await , await . , , await, , . , , , -.

, , async/await. , , - , . . I/O, , ( , HttpClient), , ; , I/O.

, parallelism - . async/await , , , , , , " ", parallelism.

, , , . , - async/wait intro, - .

+3

async/await , Task.ContinueWith.ContinueWith.ContinueWith ..

, Task.ContinueWith, , .

, ,

    public static void Main()
    {
        Launch();
    }
    public static async void Launch()
    {
        Console.WriteLine("Hello!! welcome to task application");
        Console.ReadKey();
        Console.WriteLine(await GetMessage());
        Console.ReadKey();
    }

    public static Task<string> GetMessage()
    {
        return Task.Factory.StartNew<string>(() =>
            {
                return "Good Job";
            });
    }

:

    public static void Main()
    {
        Launch();
    }
    public static async void Launch()
    {
        Console.WriteLine("Hello!! welcome to task application");
        Console.ReadKey();
        return  Task.Factory.StartNew(() => GetMessage())
            .ContinueWith((t) => 
                  {
                     Console.WriteLine(t.Result)
                     Console.ReadKey();
                  });
    }

    public static Task<string> GetMessage()
    {
        return Task.Factory.StartNew<string>(() =>
            {
                return "Good Job";
            });
    }

, GetMessage() ContinueWith, . , .

, :

Launch().Wait();

ContinueWith() , , , , "" .

, , await, TPL , .

, , . async/await /ContinueWith.

async/ TPL . .

+3

All Articles