none
不使用await关键字 能否调用async 方法? RRS feed

  • Pergunta

  •        private async void StartBtn_Click(object sender, RoutedEventArgs e)
            {
                Uri url = new Uri(Setting_Url.Text, UriKind.Absolute);
                var SingleRequest = new SingleRequest(url);
                
                
                string result = await SingleRequest.GetString() ;//如果这个方法不是 button Click 而是 Main方法,该如何调用
                OutPut.Text = result;
            }

    这个 GetString是一个返回 Task<string> 类型的 async 方法,里面调用了 webrequest.GetResponseAsync() ;

    如何在不使用 await的情况下调用?

    domingo, 24 de janeiro de 2016 08:33

Respostas

  • 你好,John . King

    根据下面的类似问题的帖子中提及到的博客你可以知道,最好还是要使用await 来调用异步方法。否则,将出现页面锁死的状态。我已经在我电脑上做了测试,确实出现了这个问题。

    http://stackoverflow.com/questions/28601678/calling-async-method-on-button-click

    其他提到,可以不在控制台应用程序中使用await,你可以参考如下代码。

                Uri url = new Uri(@"http://www.baidu.com", UriKind.Absolute);
                var SingleRequest = new SingleRequest(url);
                string resultAsync = SingleRequest.GetString().Result;
                Console.WriteLine(resultAsync);

    我的类SingleRequest 的定义如下。

        public class SingleRequest
        {
            private static WebRequest request;
            public SingleRequest(Uri uri)
            {
                request = WebRequest.Create(uri);
            }
            public static async Task<string> GetString()
            {
                var response = await request.GetResponseAsync();
                using (var stm = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stm))
                    {
                        var content = await reader.ReadToEndAsync();
                        return content;
                    }
                }
            }
        }

    Best Regards,

    Albert Zhang



    • Editado Albert_Zhang segunda-feira, 25 de janeiro de 2016 13:01
    • Marcado como Resposta John . King terça-feira, 16 de fevereiro de 2016 03:01
    segunda-feira, 25 de janeiro de 2016 12:59
  • ?????
    • Marcado como Resposta CaillenModerator terça-feira, 16 de fevereiro de 2016 02:07
    • Editado [-] quinta-feira, 11 de janeiro de 2018 13:35
    segunda-feira, 15 de fevereiro de 2016 07:36

Todas as Respostas

  • 你好,John . King

    根据下面的类似问题的帖子中提及到的博客你可以知道,最好还是要使用await 来调用异步方法。否则,将出现页面锁死的状态。我已经在我电脑上做了测试,确实出现了这个问题。

    http://stackoverflow.com/questions/28601678/calling-async-method-on-button-click

    其他提到,可以不在控制台应用程序中使用await,你可以参考如下代码。

                Uri url = new Uri(@"http://www.baidu.com", UriKind.Absolute);
                var SingleRequest = new SingleRequest(url);
                string resultAsync = SingleRequest.GetString().Result;
                Console.WriteLine(resultAsync);

    我的类SingleRequest 的定义如下。

        public class SingleRequest
        {
            private static WebRequest request;
            public SingleRequest(Uri uri)
            {
                request = WebRequest.Create(uri);
            }
            public static async Task<string> GetString()
            {
                var response = await request.GetResponseAsync();
                using (var stm = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stm))
                    {
                        var content = await reader.ReadToEndAsync();
                        return content;
                    }
                }
            }
        }

    Best Regards,

    Albert Zhang



    • Editado Albert_Zhang segunda-feira, 25 de janeiro de 2016 13:01
    • Marcado como Resposta John . King terça-feira, 16 de fevereiro de 2016 03:01
    segunda-feira, 25 de janeiro de 2016 12:59
  • 这真是个恼人的问题,  如果你要用 await, 那你的方法就必须是  async的, 而你要 调用这个方法, 调用的方法也要是 async, 调用这个的方法也要是 async 的 ..... 最后,都是 async的了;

    还有一点就是  await 好像并不能提高 程序的运行速度, 毕竟还是要等待,这样的等待跟 同步执行有什么区别?

    terça-feira, 26 de janeiro de 2016 02:48
  • 你好,John . King

    最近我找到了一篇关于C# Async await 的博客,你可以参考下。Async异步编程最大的一个特点就是传播性,即如果有一个异步方法,则所有调用这个方法的方法都应该是异步方法。

    通常,我们所说的同步并不是并行操作的概念。比如,在浏览网页时候,如果页面是同步获取数据,我们在点击按钮后就需要一直等到完成了才能继续下面的操作。而异步则是在我们点击按钮后,可以继续后面的操作,等数据加载完成后自动修改页面,而不会打断用户操作。

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Albert Zhang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    quarta-feira, 27 de janeiro de 2016 02:44
  • 哈,我最初也很疑惑,一直在想最底层的 异步方法 是怎么编写的。(●'◡'●)
    sábado, 6 de fevereiro de 2016 08:36
  • 我认为 async 并不能提高性能,  这张图上面,应该是 手动建立3个task运行的时间, 而下面是用 await 调用3次的时间; 不知道我又没有理解错?


    • Editado John . King segunda-feira, 15 de fevereiro de 2016 01:24
    segunda-feira, 15 de fevereiro de 2016 01:23
  • 您好!

         async和await关键字需要成对使用

         另外,关于性能,异步无法从根本上提升性能,只是减少某些任务长期阻塞的现象。


    周雪峰

    segunda-feira, 15 de fevereiro de 2016 05:33
    Moderador
  • ?????
    • Marcado como Resposta CaillenModerator terça-feira, 16 de fevereiro de 2016 02:07
    • Editado [-] quinta-feira, 11 de janeiro de 2018 13:35
    segunda-feira, 15 de fevereiro de 2016 07:36