Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there!

Quick question - recently I been working on an older project where every method is of type Task<t> and 95% of the stuff is synchronous. Meaning it always waits for the task to complete before going further. So - is there any benefit of having that instead just converting the method to synchronous and removing the "await" keyword?


What I have tried:

google stuff, msdn stuff, whatever
Updated 24-May-20 21:55pm

1 solution

When using Tasks you could use a loop and check if the Task .IsCompleted in the loop, this way you don't have to wait.
Task.IsCompleted Property (System.Threading.Tasks) | Microsoft Docs[^]

I used this in a Windows service without any Async statements and about 20 simultaneous running Tasks (including long running tasks) for monitoring all kinds of things, and it works without any problems.

There is a nice series of articles on CodeProject about Tasks: Task Parallel Library: 1 of n[^]
Share this answer
MK-Gii 25-May-20 6:06am    
But in general - is there any benefit is using TPL for purely synchronous flows?
RickZeeland 25-May-20 6:26am    
Probably not if all tasks must be handled sequentially ... if you are using an UI like winforms however it can be useful to keep the UI responsive.
MK-Gii 25-May-20 7:14am    
In my case it was just a web API, where calls to db to get something from it was made async and then waiting each time for the data before going further. So the API consumer still has to wait same amount of time and I need to make sure not to forget to await each time I try get something from db.
Richard Deeming 27-May-20 15:43pm    
If the code is actually async - in this case, it sounds like it's IO-bound - then there is a benefit.

Rather than having the calling thread "freeze" until the IO-bound operation has completed, the work it was doing can be suspended and the thread can be returned to the pool and used to process other tasks. When the operation has completed, a free thread can then be obtained to continue with the work.

Think of it like the difference between having the kids in the back of the car shouting "Are we nearly there yet? Are we nearly there yet?" over and over again for the entire journey, and having them sat quietly looking at their phones until you tell them you've arrived. :)
RickZeeland 27-May-20 16:17pm    
Haha, nice comparison, now over to the launch of SpaceX !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900