1

Can someone please explain me the difference between those two blocks.

This one executes about 2 seconds (that means that awaits are asynchronous.):

 [Test] public async void TestAwait() { var sw = new Stopwatch(); sw.Start(); var task = TestAwaiter(5, 2000).ConfigureAwait(false); var task1 = TestAwaiter(10, 2000).ConfigureAwait(false); var i = await task; var j = await task1; Console.WriteLine(i+j); Console.WriteLine(Math.Round(sw.Elapsed.TotalSeconds, 0)); Assert.AreEqual(Math.Round(sw.Elapsed.TotalSeconds, 0), 2); sw.Stop(); } public async Task<int> TestAwaiter(int num, int waitTimeSec) { await Task.Delay(waitTimeSec).ConfigureAwait(false); return num; } 

This one executes about 4 seconds (that means that awaits are synchronous.)

 [Test] public async void TestAwait() { var sw = new Stopwatch(); sw.Start(); var i = await TestAwaiter(5, 2000).ConfigureAwait(false); var j = await TestAwaiter(10, 2000).ConfigureAwait(false); Console.WriteLine(i+j); Console.WriteLine(Math.Round(sw.Elapsed.TotalSeconds, 0)); Assert.AreEqual(Math.Round(sw.Elapsed.TotalSeconds, 0), 2); sw.Stop(); } public async Task<int> TestAwaiter(int num, int waitTimeSec) { await Task.Delay(waitTimeSec).ConfigureAwait(false); return num; } 

I can't understand how they are different. Why assigning the await for task later then creating it affects the execution order.

2
  • What test runner are you using? How is that test runner keeping track of running task if you test methods are async void? Commented Apr 13, 2015 at 10:10
  • NUnit, works flawlessly. Commented Apr 18, 2015 at 17:41

2 Answers 2

3

First one starts both tasks before await'ing them, second one "waits" till first task finishes and than starts the second one.

First code sample:

// starting 2 tasks at 0 seconds var task = TestAwaiter(5, 2000).ConfigureAwait(false); var task1 = TestAwaiter(10, 2000).ConfigureAwait(false); // waiting for task to finish (about 2 second) var i = await task; // waiting for task1 to finish (likely 0 seconds as already waited 2 from 0) var j = await task1; 

Second:

// waiting for task to finish (about from 0 to 2 second) var i = TestAwaiter(5, 2000).ConfigureAwait(false); // waiting for task to finish (about from 2 to 4 second) var j = TestAwaiter(5, 2000).ConfigureAwait(false); 
Sign up to request clarification or add additional context in comments.

Comments

1

An async method runs synchronously until the first await is reached. It then logically takes the awaited task, adds the rest of the method as a continuation and returns a task representing the entire operation.

When you await each task you wait asynchronously for the delay to complete before starting the second one. When you don't the delays happen concurrently (so they take half the time they would if run one after the other).

You can simplify the example with this:

var a = Task.Delay(2000); var b = Task.Delay(2000); await a; // Waits 2 seconds for first delay. await b; // Finished immediately as 2 seconds passed. 

VS:

await Task.Delay(2000); // Waits for 2 seconds await Task.Delay(2000); // Waits for another 2 seconds 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.