After reading lot of articles and this forum itself i came to some conclusion i would like to confirm. Please of your answer. Let's assume our example async
method presents as below. Please read also comments in code i placed. Let's assume that in this case both getStringTask
and DoIndependentWork()
takes quick therefore entire async
method AccessTheWebAsync()
would take quick job:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com"); //assume it is quick job http request
DoIndependentWork(); //assume it is quick job for loop
string urlContents = await getStringTask;
return urlContents.Length;
}
Then calls from diffrent places would look like:
//example caller from some UI : either Froms or WPF
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
Task<int> getLengthTask = AccessTheWebAsync(); //it' call from UI but but still no need await Task.Run(AccessTheWebAsync) just await cause AccessTheWebAsync takes quick job
SomeIndependentWorkMethodHere;.
int contentLength = await getLengthTask ();
}
//Call from Console application
private async static void Main()
{
Task<int> getLengthTask = AccessTheWebAsync();
SomeIndependentWorkMethodHere;.
int contentLength = await getLengthTask (); //no need Task.Run() at all just await
}
//Call from project library or aspnet controller
private static void MyMethod()
{
Task<int> getLengthTask = AccessTheWebAsync();
SomeIndependentWorkMethodHere;.
int contentLength = await getLengthTask (); //no need Task.Run() at all just await
}
Is this true that because our async method is taking quick job no matter from where it is called either UI/library/console app it is just need to use call like below means:
await getLengthTask ();
Let's now consider same situation but consider either both of operations in async method would take long or just one of them:
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("https://msdn.microsoft.com"); //assume it is long job http request
DoIndependentWork(); //assume it is long job for loop
string urlContents = await getStringTask;
return urlContents.Length;
}
Does this change situation to this? :
//example caller from some UI : either Froms or WPF
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
SomeIndependentWorkMethodHere;.
await Task.Run(() => AccessTheWebAsync()); // <------ NOW additional Task.Run() is placed
}
//Call from Console application
private async static void Main()
{
Task<int> getLengthTask = AccessTheWebAsync(); // <------- no need to change it's console even if async method is long (just await)
SomeIndependentWorkMethodHere;.
int contentLength = await getLengthTask ();
}
//Call from project library or aspnet controller
private static void MyMethod()
{
Task<int> getLengthTask = AccessTheWebAsync(); // <------- no need to change it's library project (or asp.net controller) even if async method is long (just await)
SomeIndependentWorkMethodHere;.
int contentLength = await getLengthTask ();
}
In this case for UI
related call Task.Run
additionally has been added on await
method because it's UI
and we operate on long running async method
.
For rest console app
and library project
(or even for asp.net controller
) signature still not changed.
Is my understanding correct? Summarizing it would mean only from UI related calls i need to add additional Task.Run
so await Task.Run(asyncMethod)
BUT ONLY IF if the async
method as a whole could take a long time
otherwise await would be just enough. But for rest library projects, console apps, asp.net controllers it's always just to use withotut Task.Run
means await asynMethod() no matter either async method would take quick job or not.