如何正确地写异步方法? [英] How to correctly write async method?

查看:140
本文介绍了如何正确地写异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想学习使用异步,并在C#中等待的基础知识,但我不知道我在做什么错在这里。我期待下面的输出:

 电话DoDownload
DoDownload DONE
[...这里输出...]

但我没有得到下载的输出,同时我也希望做,但需要一段时间。难道不应该立即输出?另外,我似乎无法要么得到的字符串结果。这里是我的code:

 命名空间AsyncTest
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            的Debug.WriteLine(调用DoDownload);
            DoDownloadAsync();
            的Debug.WriteLine(DoDownload完成);
        }        私有静态异步无效DoDownloadAsync()
        {
            WebClient的W =新的WebClient();            字符串的txt =等待w.DownloadStringTaskAsync(http://www.google.com/);
            的Debug.WriteLine(TXT);
        }
    }
}


解决方案

要得到你想要的,你需要等待过程中,你退出之前完成的行为的Main()。为了能告诉你的进程做了你需要从你的函数返回的工作代替无效,你永远不应该返回无效异步的功能,除非你正在处理事件。

你的程序重新编写的版本,正常工作将是

 类节目
{
    静态无效的主要(字串[] args)
    {
        的Debug.WriteLine(调用DoDownload);
        变种downloadTask = DoDownloadAsync();
        的Debug.WriteLine(DoDownload完成);
        downloadTask.Wait(); //等待后台任务完成前完成。
    }    私有静态异步任务DoDownloadAsync()
    {
        WebClient的W =新的WebClient();        字符串的txt =等待w.DownloadStringTaskAsync(http://www.google.com/);
        的Debug.WriteLine(TXT);
    }
}

由于你不能等待的Main()我不得不做的 等待() 函数。如果这是有一个<一个一个应用程序href=\"http://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext%28v=vs.110%29.aspx\"相对=nofollow>的SynchronizationContext 我会做等待downloadTask; 来代替,使这个正在从调用的函数异步

So I am trying to learn the basics of using 'async' and 'await' in C#, but I am not sure what I am doing wrong here. I am expecting the following output:

Calling DoDownload
DoDownload done
[...output here...]

But I don't get the output of the download, and I also expect "done" but that takes a while. Shouldn't that be output immediately? Also, I can't seem to get the string result either. Here is my code:

namespace AsyncTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Debug.WriteLine("Calling DoDownload");
            DoDownloadAsync();
            Debug.WriteLine("DoDownload done");
        }

        private static async void DoDownloadAsync()
        {
            WebClient w = new WebClient();

            string txt = await w.DownloadStringTaskAsync("http://www.google.com/");
            Debug.WriteLine(txt);
        }
    }
}

解决方案

To get the behavior you want you need to wait for the process to finish before you exit Main(). To be able to tell when your process is done you need to return a Task instead of a void from your function, you should never return void from a async function unless you are working with events.

A re-written version of your program that works correctly would be

class Program
{
    static void Main(string[] args)
    {
        Debug.WriteLine("Calling DoDownload");
        var downloadTask = DoDownloadAsync();
        Debug.WriteLine("DoDownload done");
        downloadTask.Wait(); //Waits for the background task to complete before finishing. 
    }

    private static async Task DoDownloadAsync()
    {
        WebClient w = new WebClient();

        string txt = await w.DownloadStringTaskAsync("http://www.google.com/");
        Debug.WriteLine(txt);
    }
}

Because you can not await in Main() I had to do the Wait() function instead. If this was a application that had a SynchronizationContext I would do await downloadTask; instead and make the function this was being called from async.

这篇关于如何正确地写异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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