C# async,await

class Program
{
static void Main(string[] args)
{
(new Program()).MyMethodAsync();
Console.ReadLine();
}
public async Task MyMethodAsync()
{
Task<int> task = AccessTheWebAsync();
int result = await task;
Console.WriteLine(result);
}
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
string urlContents = await getStringTask;
return urlContents.Length;
}
}

posted @ 2016-08-17 22:31  FredGrit  阅读(127)  评论(0编辑  收藏  举报