【问题标题】:How to write simple async method?如何编写简单的异步方法?
【发布时间】:2011-07-21 07:42:59
【问题描述】:

使用最新的 CTP5 和 async/await 关键字,我写了一些代码,显然无法编译:

 class Program
    {
        public class MyClass
        {
            async public Task<int> Test()
            {
                var result = await TaskEx.Run(() =>
                    {
                        Thread.Sleep(3000);
                        return 3;
                    });
                return result;
            }
        }

        static void Main(string[] args)
        {
            var myClass = new MyClass();

            //The 'await' operator can only be used in a method or lambda marked with the 'async' modifier error ??!!
            int result = await myClass.Test();

            Console.ReadLine();
        }
    }

“'await' 运算符只能用在标有 'async' 修饰符错误的方法或 lambda 中”的原因是什么? (我选择了 Visual Studio 指向我的行)

【问题讨论】:

    标签: c# async-ctp


    【解决方案1】:

    我不知道您是否可以将 Main 标记为异步,但您需要在任何使用 await 的方法的声明中包含 async 关键字。例如:

    public async void DoStuffAsync ()
    {
        var myClass = new MyClass ();
    
        int result = await myClass.TestAsync ();
    }
    

    【讨论】:

    • 可以Main声明为async,并且awaiting其中的任何内容结束你的程序(假设你没有其他前台线程正在运行。)
    • 呃,我一直认为错误在我的测试方法中。 Visual Studio 应该提供更多有关此错误的信息(例如“方法 Main 必须异步”)
    • 在 C#5 版本中,如果将 Main 标记为 async,则会收到错误:“无法使用 'async' 修饰符标记入口点”
    【解决方案2】:

    awaitWait()不一样;执行await 是对该方法的重大重写,尤其会影响对该方法如何退出调用者的期望。你是对的,它实际上并没有太多(警告:返回类型),除了告诉编译器启用某些东西(就像unsafecheckedunchecked 这样的开关如果您考虑一下)-但请考虑:在您的示例中,这实际上很重要。如果Main() 退出(并且我们假设没有其他线程) - 你的 exe is toast。走了。不复存在。添加async 让您认为仅仅因为方法exits 并不意味着它已经完成。你真的不希望Main() 在你准备好之前退出。

    作为次要效果,此开关还形式化了该方法只能返回 Task 之类的内容;如果没有这个开关,你可能会想在以后让它异步,这可能是一个重大的改变。

    【讨论】:

    • @dlev 不错的 Monty Python ;p
    【解决方案3】:

    异步方法的返回类型可以是 void 或 Task。如果返回类型不是 void,调用者仍然可以在 Main 入口方法中使用 .Net 4 中引入的标准等待机制(不能标记为异步)。这是一个简单的例子:

        static void Main(string[] args)
        {
            string address = "http://api.worldbank.org/countries?format=json";
            Task t = LoadJsonAsync(address);
            // do other work while loading
            t.Wait();
    
            Console.WriteLine("Hit ENTER to exit...");
            Console.ReadLine();
        }
    
        private async static Task LoadJsonAsync(string address)
        {
            HttpClient client = new HttpClient();
    
            HttpResponseMessage response = await client.GetAsync(address);
    
            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();
    
            // Read response asynchronously as JsonValue and write out top facts for each country
            JsonArray readTask = await response.Content.ReadAsAsync<JsonArray>();
            Console.WriteLine("First 50 countries listed by The World Bank...");
            foreach (var country in readTask[1])
            {
                Console.WriteLine("   {0}, Capital: {1}",
                    country.Value["name"],
                    country.Value["capitalCity"]);
            }
    
        }
    

    【讨论】:

    • 我讨厌每个人总是使用 client.GetAsync 作为示例。发帖人可能知道如何使用别人的异步方法。你如何一个? (例如使用睡眠示例)
    • 如果你的异步任务返回结果怎么办?调用 task.wait 返回一个布尔值。您将如何等待然后使用返回的结果?编辑:没关系。 Task.Result.
    • 不确定,但您不需要在 Wait() 之前启动 Task t 吗? t.Start(); t.Wait();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多