I found the following example from Microsoft website about Async and await ( https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/):
// 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("https://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;
}
My first question is from the webpage "If AccessTheWebAsync doesn't have any work that it can do between calling GetStringAsync and awaiting its completion, you can simplify your code by calling and awaiting in the following single statement." It seems to imply that the following statement is really doing something:
Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");
Is that true? I though it's just a definition of a task, but it has not yet started/run.
Second question is I tried to change the implementation as follows to check my idea:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
//Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com");
Task<string> getStringTask = DoPriorWork();
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
string urlContents = await getStringTask;
return urlContents.Length;
}
void DoIndependentWork()
{
resultsTextBox.Text += "Working . . . . . . .\r\n";
}
Task<string> DoPriorWork()
{
return new Task<string>(
() =>
{
string retStr = "good";
Debug.Assert(true, "running dopriorwork");
System.Threading.Thread.Sleep(10000);
return retStr;
}
);
}
However, the method DoPriorWork() didn't execute at all. What is wrong with this implementation? Thanks!