码迷,mamicode.com
首页 > 其他好文 > 详细

c# async

时间:2014-06-21 17:58:34      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   ext   color   

• Methods (as well as lambda expressions or anonymous methods) can be marked with the async keyword to enable the method to do work in a nonblocking manner.
• Methods (as well as lambda expressions or anonymous methods) marked with the async keyword will run in a blocking manner until the await keyword is encountered.
• A single async method can have multiple await contexts.
• When the await expression is encountered, the calling thread is suspended until the awaited task is complete. In the meantime, control is returned to the caller of the method.
• The await keyword will hide the returned Task object from view, appearing to directly return the underlying return value. Methods with no return value simply return void.
• As a naming convention, methods that are to be called asynchronously should be marked with the “Async” suffix.

Call the async methode, get the task object and await later:

Task<string> ts = WorkAsync();
//Do some independent work 
...
//now wait for WorkAsync 
tb.Text = await ts;

 
await with timeuot <1>:

if (await Task.WhenAny(task, Task.Delay(delay)) == task)
{ ... }
else 
{ //timeout  ... }

 

c# async,布布扣,bubuko.com

c# async

标签:style   class   blog   code   ext   color   

原文地址:http://www.cnblogs.com/leihdm/p/3800221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!