使用异步和等待关键字的好处 [英] Benefits of using async and await keywords

查看:71
本文介绍了使用异步和等待关键字的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中使用异步方法是我的新手.我已经读到这些关键字asyncawait通过使某些方法异步来帮助使程序更具响应性.我有这个片段:

I'm new in the use of asynchronous methods in C#. I have read that these keywords async and await help to make the program more responsive by asynchronizing some methods. I have this snippet :

第一条路

    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();
    }

第二种方式

 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";
                });
        }

我需要知道:

  1. 两个实现之间(在并行性概念上)是否有所不同?

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

如果我只能创建一个任务并等待其完成,那么使用asyncawait关键字有什么好处?

What are the benefits of using async and await keywords if I can just create a task and wait for it to finish?

推荐答案

假设您有一个边界检查点.每辆车都可以一次通过,让海关检查一下自己的车,看看他们是否没有走私比利时的巧克力.

Say you have a single border checkpoint. Each car can pass it one-by-one to have customs take a look at their car to see if they're not smuggling any Belgian chocolate.

现在,假设您已经排在大众甲壳虫的行列中,那么在成为24轮怪兽卡车之前,您几乎无法适应.您现在已经在这个庞然大物后面呆了很长时间,直到海关在对它进行全面搜索之后,他们才可以继续向您求婚,他们基本上只需要轻拍一下就可以告诉您,您很好.

Now assume that you are in line in your Volkswagen Beetle where you can barely fit in and before you is a 24-wheel monstertruck. You are now stuck behind this behemoth for a long time until customs are done searching through it all before they can move on to you who they basically just have to pat down to tell you you're good to go.

为了对抗这种效率,我们在边境巡逻的好朋友有了一个主意,并安装了第二个检查站.现在他们可以传给两倍的人数,而您只需接一个,而不用等到怪物卡车后面!

In order to combat this efficiency, our good friends at the border patrol have an idea and install a second checkpoint. Now they can pass in twice as many people and you can just take that one instead of waiting behind the monstertruck!

问题解决了吧?不完全是.他们忘记创建通往该检查站的第二条道路,因此所有交通仍必须经过单个车道,从而导致卡车仍然挡住了甲壳虫.

Problem solved, right? Not exactly. They forgot to create a second road that leads to that checkpoint so all traffic still has to go over the single lane, resulting in the truck still blocking the Beetle.

这与您的代码有何关系?非常简单:您正在做同样的事情.

How does this relate to your code? Very easy: you're doing the same.

创建新的Task时,实际上是创建了第二个检查点.但是,当您现在使用.Wait()同步阻止它时,您将迫使所有人都走这条路.

When you create a new Task you essentially create that second checkpoint. However when you now synchronously block it using .Wait(), you are forcing everyone to take that single road.

在第二个示例中,您使用await创建第二条道路,并允许您的汽车与卡车同时处理.

In the second example you use await which creates that second road and allows your car to be handled simultaneously with the truck.

这篇关于使用异步和等待关键字的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆