Skip to content

iarekk/await-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

This program tests two ways to run a Fire&Forget async call in C#

The CpuThenIo method is designed to spend about 1200ms doing CPU-intensive work, and then a Task.Delay(1000) simulates an IO-bound call.

The wrong method to do this is _ = CpuThenIo(numberOfCycles);. This will cause the calling thread to wait until the CPU-bound work is completed, and then the calling thread resumes execution, paralled with the IO call.

The right way is _ = Task.Run(() => CpuThenIo(numberOfCycles));. This allows the calling thread to resume immediately, while both CPU and IO work happen in the background.

Invoking a CPU/IO method that takes about 1200ms CPU work and 1000ms IO work
WRONG: `_ = CpuThenIo(numberOfCycles);`
Starting outer
* * Starting inner
* * * * Starting CPU
* * * * Finished CPU: 1194ms
* * * * Starting IO
Finished outer: 1197ms
NOTE that outer exited after CPU but before IO
* * * * Finished IO 2197ms
* * Finished inner: 2197ms
------------------------------------------------------------
RIGHT: `_ = Task.Run(() => CpuThenIo(numberOfCycles), canToken.Token);`
Starting outer
Finished outer: 1ms
NOTE that outer exited before both CPU and IO, which is correct for Fire & Forget
* * Starting inner
* * * * Starting CPU
* * * * Finished CPU: 1372ms
* * * * Starting IO
* * * * Finished IO 2374ms
* * Finished inner: 2374ms

System information:

> dotnet --version                                                                                                                                                     <aws:sts>
6.0.100

> uname -a                                                                                                                                                             <aws:sts>
Darwin PHXKOVTIA01-MAC 19.6.0 Darwin Kernel Version 19.6.0: Tue Oct 12 18:34:05 PDT 2021; root:xnu-6153.141.43~1/RELEASE_X86_64 x86_64

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages