Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
We're improving our VB.NET solution by Async'ing most functions. Adding Async/Await is pretty easy now but we're confused on how to proceed with the following.
At application launch we pre-populate several (10) datatables from Azure SQL DB (we have a progress bar that increments its value as each sync function is done). We can add Async/Await on each function but we thought we could try and run them all at the same time and just wait on the last one to finish before we continue (thought this would speed up load time). All functions are SQL SELECT commands.
We've read quite a bit on this but didn't really find what we need. Thought we could use Async/Await only and no backgroundworkers nor tasks.
Combining the 10 Sync functions in 1 Async function is not a solution as we still need to run them individually later on.
How would you go about and nail this? Very much appreciate your comments.
Thanks!
Sergio

What I have tried:

Tried making each individual function Async but we're after initial performance gains by running the 10 SELECTs simultaneously.
Posted
Updated 2-Nov-18 13:44pm

1 solution

Quote:
just wait on the last one to finish
That is a bit of confusing statement. How do you say which is the last one? Do you have a reference to the last one in a Task variable? Or, you have a list of them, and you just want to see when all of them have completed? Well, both of these can be solved easily using the TPL (Task Parallel Library).
C#
var lastTask = something;

// When all is scheduled
lastTask.Wait(); 

// Or await lastTask if the function is async.
This will wait only for the last task, if any other tasks are pending, it will skip. However, a better alternative is, to have a List<Task> and have the Task function for the WhenAll to be called on it.
C#
var list = allTasks;

Task.WhenAll(list).Wait(); // or await Task.WhenAll(list);
You are already consuming async/await pattern, so you know that async and await approach is better. But somehow you also seem to be a bit confused in this,
Quote:
Thought we could use Async/Await only and no backgroundworkers nor tasks.
Async/await pattern in C# is based on Task (System.Threading.Tasks namespace). So, I don't think you can apply anything with async/await, and skip the Task type. :-)

Task.WhenAll Method (System.Threading.Tasks) | Microsoft Docs[^]
Quote:
10 SELECTs simultaneously.
That's like 10 new requests to the Azure SQL Database, 10 connections (even though backend can utilize connection pooling), and then an overhead of the threads, thread pools and context switching. I am unsure as to how this could have improved the overall startup time and performance. Yes, it can improve responsiveness and scalability, and utilize the thread pool threads, but, still. 10 SELECT commands are going to take the same amount of time they are going to take in sync version, but yes, resource creation is going to be a lot cheaper in asynchronous approach.
 
Share this answer
 
Comments
sterenas 2-Nov-18 20:49pm    
Thanks Afzaal! By "last task" I mean the "slowest task" or the one that could take longer to run at a particular time.
Even if we only improve responsiveness and scalability, that's already a quick win but I'd still hope it should run the 10 SELECTs at the same time rather than sequentially, right ?
Using connection pooling on Azure SQL Database and plenty DTUs available.
You still say we shouldn't notice performance gain?
Thanks!
Sergio
Afzaal Ahmad Zeeshan 2-Nov-18 22:10pm    
Then the list method should work for you.

I was speaking about sync/async approach. It depends on the app itself too, what is this application? In the case of a web application, an async approach can increase scalability, by a factor. But remember, each operation itself will take its own time as much as it demands.

In other words, app will be more efficient in handling requests, and generating responses. But the overall request time will remain the same whatsoever because async does not guarantee any faster execution of the code.
BillWoodruff 2-Nov-18 22:30pm    
+5
Afzaal Ahmad Zeeshan 2-Nov-18 22:31pm    
Thank you, Bill.

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