Answered by:
Task await, async

Question
-
Hello,It's not so clear to me, Task await, async.
I create a task and still wait.
Then it's not parallel, asynchronous. But what is the advantage?
My test call doesn't work either.
Can someone please help me?public class Possibilities() { // --------------- TASK await private int MyCalculation() { int res = 0; for (int i = 0; i < 50; i++) { res += i; //Simulate Thread.Sleep(50); Console.WriteLine("Calc + " + i.ToString()); } return res; } public Task<int> MyCalculationAsync() { //return Task.Factory.StartNew<int>(() => MyCalculation()); return Task.Factory.StartNew(() => MyCalculation()); } public void AwaitTaskAsynAsync() { // int summe = MyCalculation(); int summe = await MyCalculationAsync(); } Main.cs static void Main(string[] args) { Possibilities test = new Possibilities(); test.AwaitTaskAsynAsync(); }
Many greetings MarkusTuesday, January 22, 2019 5:14 PM
Answers
-
Hi Markus,
Yes, you are right, if you have solved your issue, please remember to close your thread by clicking Mark as answer, it is beneficial to other community members who face the same issue.
Thanks for your understanding.
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Markus Freitag Thursday, January 24, 2019 5:52 PM
Thursday, January 24, 2019 1:33 AM
All replies
-
Hi,
With async await. The benefit is that the UI thread is free'd up whilst your task is running (in your case MyCalculation). So this stops your application freezing until the operation completes.
The .Net framework decides if the task should be run in parallel for you you do not decide.
There are loads of articles related to do this subject on the internet so I dont really want to copy and paste into this thread.
just type this into google
async multithreading, C#
and you will learn more about it.
hope that helps?
Tuesday, January 22, 2019 6:05 PM -
Hi Markus,
Firstly, your code have some problems, I modify your code, :
class Program { static void Main(string[] args) { Possibilities test = new Possibilities(); test.AwaitTaskAsynAsync(); Console.WriteLine("this is test"); Console.ReadKey(); } } public class Possibilities { private int MyCalculation() { int res = 0; for (int i = 0; i < 50; i++) { res += i; //Simulate Thread.Sleep(5); Console.WriteLine("Calc + " + i.ToString()); } return res; } public Task<int> MyCalculationAsync() { //return Task.Factory.StartNew<int>(() => MyCalculation()); return Task.Factory.StartNew(() => MyCalculation()); } public async void AwaitTaskAsynAsync() { // int summe = MyCalculation(); int summe = await MyCalculationAsync(); Console.WriteLine("this is asymc method"); } }
Using Async to mark
AwaitTaskAsynAsync
as an asynchronous method, and use Await to mark MyCalculationto indicate that it takes time-consuming operations. When the main thread encounters await, it will return immediately, and continue to execute the logic under the main thread in a non-blocking form. When the await time-consuming operation is completed, continue to execute the logic under AwaitTaskAsynAsync.About Asynchronous programming with async and await, you can take a look:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, January 23, 2019 5:57 AM -
Hi Cherry,It's working, thank you very much.
I only use this feature when I calculate a lot and the user
input is not blocked. Since I'm waiting, it's like in sync.
Do I see that right?Of course, there are loads of articles related to do this subject on the internet.Best Regards MarkusWednesday, January 23, 2019 5:28 PM -
Hi Markus,
Yes, you are right, if you have solved your issue, please remember to close your thread by clicking Mark as answer, it is beneficial to other community members who face the same issue.
Thanks for your understanding.
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Marked as answer by Markus Freitag Thursday, January 24, 2019 5:52 PM
Thursday, January 24, 2019 1:33 AM