C
C#3y ago
Nemesis

Call to async method waiting for execution even without await

I am trying to execute a async method multiple times in parallel. For this I am using the following code:
c#
foreach (var name in Names)
{
tasks[i] = Method(name);
}
var results = await Task.WhenAll(tasks.Where(task => task != null));
c#
foreach (var name in Names)
{
tasks[i] = Method(name);
}
var results = await Task.WhenAll(tasks.Where(task => task != null));
I added a sleep inside the method to verify parallel execution. But when I debug the code, the debug waits for the sleep time on the method call before moving ahead. What am I doing wrong here?
17 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Nemesis
NemesisOP3y ago
but shouldn't the sleep be executed in a separate thread?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
canton7
canton73y ago
Using async doesn't by itself add new threads Async lets a thread pick up another bit of work when it hits an await, rather than sitting there blocked
Nemesis
NemesisOP3y ago
oh, so sleep will block all the threads
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
canton7
canton73y ago
There's only one thread here
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Nemesis
NemesisOP3y ago
ok, so my code wont execute the multiple Method calls in parallel? on changing Sleep to Thread.Delay, it is no longer waiting for the time, but it waits at the await Task.WhenAll
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Brady Kelly
Brady Kelly3y ago
How to: Write a Simple Parallel.For Loop
Learn to write Parallel.For loops in .NET in which you don't need to cancel the loop, break out of loop iterations, or maintain any thread-local state.
canton7
canton73y ago
Correct. There's one thread (which is the one which called into the method originally), and it's keeping itself busy executing another bit of code every time it hits an await
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
mikernet
mikernet3y ago
async methods do not yield the thread until the first await is hit And even then it may not yield if the awaited task can complete synchronously, i.e. you are trying to read from a network stream where the data is immediately available in a buffer It only yields the thread to continue if it actually needs to wait on something so, for example:
public Task ProcessStreamDataAsync()
{
while (true)
{
var data = await _networkStream.ReadDataAsync();

if (data == null)
break;

ProcessData(data);
}
}
public Task ProcessStreamDataAsync()
{
while (true)
{
var data = await _networkStream.ReadDataAsync();

if (data == null)
break;

ProcessData(data);
}
}
If the data is coming in fast enough such that ReadDataAsync has the data requested available to process in a local buffer immediately, this method will run fully synchronously The current thread is only yielded to continue processing if an await is hit and that await needs to actually wait on something Since everything before the first await can be processed synchronously, it always is.
Nemesis
NemesisOP3y ago
Are there any drawbacks of using Parallel.ForEach? That will create new thread for each function call right?
mtreit
mtreit3y ago
How much runs in parallel depends on the MaxDegreeOfParallelism setting and possibly other things.
Want results from more Discord servers?
Add your server