Last active
April 11, 2016 12:24
-
-
Save elitezhe/44e449476f49067cb7f842ae9be5910d to your computer and use it in GitHub Desktop.
From https://msdn.microsoft.com/en-us/library/hh191443.aspxawait 分开使用,返回值Task<>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment