首页 新闻 会员 周边

C# 异步方法

0
悬赏园豆:10 [已解决问题] 解决于 2017-10-30 10:29

在写基于任务的异步方法,遇到关于async、await的问题。

如下,写2个函数。F1()、F2(),差别是F1有async修饰。

 

问题1:

  为何F1有async修饰,就不需要return值了?而F2却提示我没有返回值?

  据我所知,async的作用是为了函数内能用await,是个编译器功能,并没有其它什么功能了啊。

 

问题2:

    明明 t 是异步执行的,为何提示却说是同步执行??

 

问题3:

  我给F2加了返回值,又写了个F3,请问F2和F3现在是不是一样的??

 

 

 

 

异步方法到底要怎么写呢????

async Task F(),才是异步方法?

Task F(),只是返回一个Task的普通方法?

萨尔的主页 萨尔 | 初学一级 | 园豆:20
提问于:2017-07-03 11:12
< >
分享
最佳答案
0

异步是要有操作的异步.像你这种写法没有任何意义.

一般有意义的异步是1.无须等待结果的并行运算.2.IO操作(数据库/文件系统/网络请求)的异步等待

你的问题 1. 这个是语法.不要问为什么.语法就是这样  2.因为你没有等待其他异步操作.整个方法自然是同步的.

3.当然不一样.代码都不一样了.  总的来说:你想太多了.

收获园豆:5
吴瑞祥 | 高人七级 |园豆:29449 | 2017-07-03 11:24

要学会总得多想点啊。。不弄清楚这个世界总感觉不够清晰

萨尔 | 园豆:20 (初学一级) | 2017-07-03 13:57

@luanwj: 你要分开学.一个是asp.net的异步语法.一个是异步编程的概念.

这个异步的概念.和语言环境都没关系.这个想明白了.再去学在.net里怎么写异步代码.

而不是揪着.net的异步语法来学.语法就是语法.就跟1+1=2一样.去想这是为什么.很浪费时间.

吴瑞祥 | 园豆:29449 (高人七级) | 2017-07-03 14:12
其他回答(5)
0

async 方法如果没有关键字await,那就是同步方法。

肖恩部落 | 园豆:585 (小虾三级) | 2017-07-03 16:52
0

 因为F2是同步方法

JCdon | 园豆:2 (初学一级) | 2017-07-04 09:38
0

 

https://msdn.microsoft.com/zh-cn/library/hh191443(v=vs.120).aspx

这个例子你应该看的很直观,异步就是不阻塞主线程。

  • async 没有这个关键字的话,调用StartButton_Click的时候会等待它执行完才会执行它下面的语句(也就是同步执行了);
  • 如果有这个async关键字且StartButton_Click方法里面有await关键字,那么需要等待StartButton_Click执行完才会执行它下面的语句(在外部感觉也是同步的,但其实在她内部是有调用异步方法的话,它内部也可以是异步的执行,只不过是外部是同步等待它执行完);
  • 如果有async关键字但StartButton_Click没有await关键字,那么就不等待StartButton_Click执行完就继续执行它下面的语句了
namespace AsyncFirstExample 
{ 
    public partial class MainWindow : Window 
    { 
        // Mark the event handler with async so you can use await in it. 
        private async void StartButton_Click(object sender, RoutedEventArgs e) 
        { 
            // Call and await separately. 
            //Task<int> getLengthTask = AccessTheWebAsync(); 
            //// You can do independent work here. 
            //int contentLength = await getLengthTask; 
 
            int contentLength = await AccessTheWebAsync(); 
 
            resultsTextBox.Text += 
                String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength); 
        } 
 
 
        // Three things to note in the signature: 
        //  - The method has an async modifier.  
        //  - The return type is Task or Task<T>. (See "Return Types" section.) 
        //    Here, it is Task<int> because the return statement returns an integer. 
        //  - The method name ends in "Async." 
        async Task<int> AccessTheWebAsync() 
        {  
            // You need to add a reference to System.Net.Http to declare client. 
            HttpClient client = new HttpClient(); 
 
            // GetStringAsync returns a Task<string>. That means that when you await the 
            // task you'll get a string (urlContents). 
            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com"); 
 
            // You can do work here that doesn't rely on the string from GetStringAsync. 
            DoIndependentWork(); 
 
            // The await operator suspends AccessTheWebAsync. 
            //  - AccessTheWebAsync can't continue until getStringTask is complete. 
            //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
            //  - Control resumes here when getStringTask is complete.  
            //  - The await operator then retrieves the string result from getStringTask. 
            string urlContents = await getStringTask; 
 
            // The return statement specifies an integer result. 
            // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
            return urlContents.Length; 
        } 
 
 
        void DoIndependentWork() 
        { 
            resultsTextBox.Text += "Working . . . . . . .\r\n"; 
        } 
    } 
} 
 
// Sample Output: 
 
// Working . . . . . . . 
 
// Length of the downloaded string: 41564. 

 

 

 

 

慧☆星 | 园豆:5640 (大侠五级) | 2017-07-04 09:38
0

问题1:为何F1有async修饰,就不需要return值了?而F2却提示我没有返回值?

           async修饰后变成了异步方法,什么是异步 要搞清楚,你知道什么是异步就知道为什么不需要return了,因为异步方法 你不知道他什么时候执行完成  你如何获取他的返回值? 异步都是靠回调函数来处理他的结果的。对于异步你要先了解。

问题2:明明 t 是异步执行的,为何提示却说是同步执行??

          你要搞清楚 async的语法规则,async本来就是和await成对出现的 ,async 标识为异步方法,await标识 需要耗时的程序代码。 你没有加await 证明你没有需要耗时的代码 ,那编译器就会告诉你 你这个方法不需要异步 我们将以同步方式执行 ,人家提示得已经很清楚了 。

问题3:我给F2加了返回值,又写了个F3,请问F2和F3现在是不是一样的??

       你这个F3 虽然加了async 但是没有await 所以 会操作问题2的 提示来执行 意思是按照同步方法处理 ,那么F2 F3就是一样的了。

如果觉得我说的对你有帮助 望采纳

收获园豆:5
丶被遗忘者 | 园豆:18 (初学一级) | 2017-08-07 14:16

答的不错,thank you

支持(0) 反对(0) 萨尔 | 园豆:20 (初学一级) | 2017-08-09 10:23
0

看看这个系列应该有助你理解。我感觉写的不错。

http://www.cnblogs.com/liqingwen/p/5831951.html

jmlsaul | 园豆:247 (菜鸟二级) | 2017-08-31 09:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册