有人可以请解释异步/等待? [英] Can somebody please explain async / await?

查看:98
本文介绍了有人可以请解释异步/等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了解异步/等待在C#5.0的,我根本不明白。我不明白它如何被用于并行。我试过下面非常基本的程序:

I'm starting to learn about async / await in C# 5.0, and I don't understand it at all. I don't understand how it can be used for parallelism. I've tried the following very basic program:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Task task1 = Task1();
            Task task2 = Task2();

            Task.WaitAll(task1, task2);

            Debug.WriteLine("Finished main method");
        }

        public static async Task Task1()
        {
            await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(5)));
            Debug.WriteLine("Finished Task1");
        }

        public static async Task Task2()
        {
            await new Task(() => Thread.Sleep(TimeSpan.FromSeconds(10)));
            Debug.WriteLine("Finished Task2");
        }

    }
}

本程序在调用只是块 Task.WaitAll()和永远不会完成。可有人请向我解释为什么?我敢肯定,我只是失去了一些东西简单或根本没有这种正确的心理模型,没有的是在那里正在帮助博客或MSDN文章。

This program just blocks on the call to Task.WaitAll() and never finishes. Can somebody please explain to me why ? I'm sure I'm just missing something simple or just don't have the right mental model of this, and none of the blogs or MSDN articles that are out there are helping.

推荐答案

我建议你开始与我的简介异步 / 等待 和后续的的 //msdn.microsoft.com/en-us/library/hh873175.aspx\">official MSDN文档。

I recommend you start out with my intro to async/await and follow-up with the official MSDN documentation on TAP.

正如我在介绍的博客文章提到,有几种工作成员是由TPL留任,并在纯异步<没有用/ code> code。 新任务 Task.Start Task.Run (或 TaskFactory.StartNew )。同样, Thread.sleep代码 Task.Delay 所取代。

As I mention in my intro blog post, there are several Task members that are holdovers from the TPL and have no use in pure async code. new Task and Task.Start should be replaced with Task.Run (or TaskFactory.StartNew). Similarly, Thread.Sleep should be replaced with Task.Delay.

最后,我建议你不要使用 Task.WaitAll ;您的控制台应用程序应该只是等待上一个工作,它使用 Task.WhenAll 。所有这些变化,你的code看起来像:

Finally, I recommend that you do not use Task.WaitAll; your Console app should just Wait on a single Task which uses Task.WhenAll. With all these changes, your code would look like:

class Program
{
    static void Main(string[] args)
    {
        MainAsync().Wait();
    }

    public static async Task MainAsync()
    {
        Task task1 = Task1();
        Task task2 = Task2();

        await Task.WhenAll(task1, task2);

        Debug.WriteLine("Finished main method");
    }

    public static async Task Task1()
    {
        await Task.Delay(5000);
        Debug.WriteLine("Finished Task1");
    }

    public static async Task Task2()
    {
        await Task.Delay(10000);
        Debug.WriteLine("Finished Task2");
    }
}

这篇关于有人可以请解释异步/等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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