├── .gitignore ├── AsyncProgramming.sln ├── CA01ThreadVsTasks ├── CA01ThreadVsTasks.csproj └── Program.cs ├── CA02TaskReturnsValue ├── CA02TaskReturnsValue.csproj └── Program.cs ├── CA03LongRunningTask ├── CA03LongRunningTask.csproj └── Program.cs ├── CA04ExceptionPropagation ├── CA04ExceptionPropagation.csproj └── Program.cs ├── CA05TaskContinuation ├── CA05TaskContinuation.csproj └── Program.cs ├── CA06TaskDelay ├── CA06TaskDelay.csproj └── Program.cs ├── CA07SyncVsAsync ├── CA07SyncVsAsync.csproj └── Program.cs ├── CA08AsyncFunctions ├── CA08AsyncFunctions.csproj └── Program.cs ├── CA09CancellationToken ├── CA09CancellationToken.csproj └── Program.cs ├── CA10ReportProgress ├── CA10ReportProgress.csproj └── Program.cs ├── CA11TaskCombinators ├── CA11TaskCombinators.csproj └── Program.cs ├── CA12ConcurrencyAndParallelism ├── CA12ConcurrencyAndParallelism.csproj └── Program.cs └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.vspscc 97 | *.vssscc 98 | .builds 99 | *.pidb 100 | *.svclog 101 | *.scc 102 | 103 | # Chutzpah Test files 104 | _Chutzpah* 105 | 106 | # Visual C++ cache files 107 | ipch/ 108 | *.aps 109 | *.ncb 110 | *.opendb 111 | *.opensdf 112 | *.sdf 113 | *.cachefile 114 | *.VC.db 115 | *.VC.VC.opendb 116 | 117 | # Visual Studio profiler 118 | *.psess 119 | *.vsp 120 | *.vspx 121 | *.sap 122 | 123 | # Visual Studio Trace Files 124 | *.e2e 125 | 126 | # TFS 2012 Local Workspace 127 | $tf/ 128 | 129 | # Guidance Automation Toolkit 130 | *.gpState 131 | 132 | # ReSharper is a .NET coding add-in 133 | _ReSharper*/ 134 | *.[Rr]e[Ss]harper 135 | *.DotSettings.user 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Coverlet is a free, cross platform Code Coverage Tool 148 | coverage*.json 149 | coverage*.xml 150 | coverage*.info 151 | 152 | # Visual Studio code coverage results 153 | *.coverage 154 | *.coveragexml 155 | 156 | # NCrunch 157 | _NCrunch_* 158 | .*crunch*.local.xml 159 | nCrunchTemp_* 160 | 161 | # MightyMoose 162 | *.mm.* 163 | AutoTest.Net/ 164 | 165 | # Web workbench (sass) 166 | .sass-cache/ 167 | 168 | # Installshield output folder 169 | [Ee]xpress/ 170 | 171 | # DocProject is a documentation generator add-in 172 | DocProject/buildhelp/ 173 | DocProject/Help/*.HxT 174 | DocProject/Help/*.HxC 175 | DocProject/Help/*.hhc 176 | DocProject/Help/*.hhk 177 | DocProject/Help/*.hhp 178 | DocProject/Help/Html2 179 | DocProject/Help/html 180 | 181 | # Click-Once directory 182 | publish/ 183 | 184 | # Publish Web Output 185 | *.[Pp]ublish.xml 186 | *.azurePubxml 187 | # Note: Comment the next line if you want to checkin your web deploy settings, 188 | # but database connection strings (with potential passwords) will be unencrypted 189 | *.pubxml 190 | *.publishproj 191 | 192 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 193 | # checkin your Azure Web App publish settings, but sensitive information contained 194 | # in these scripts will be unencrypted 195 | PublishScripts/ 196 | 197 | # NuGet Packages 198 | *.nupkg 199 | # NuGet Symbol Packages 200 | *.snupkg 201 | # The packages folder can be ignored because of Package Restore 202 | **/[Pp]ackages/* 203 | # except build/, which is used as an MSBuild target. 204 | !**/[Pp]ackages/build/ 205 | # Uncomment if necessary however generally it will be regenerated when needed 206 | #!**/[Pp]ackages/repositories.config 207 | # NuGet v3's project.json files produces more ignorable files 208 | *.nuget.props 209 | *.nuget.targets 210 | 211 | # Microsoft Azure Build Output 212 | csx/ 213 | *.build.csdef 214 | 215 | # Microsoft Azure Emulator 216 | ecf/ 217 | rcf/ 218 | 219 | # Windows Store app package directories and files 220 | AppPackages/ 221 | BundleArtifacts/ 222 | Package.StoreAssociation.xml 223 | _pkginfo.txt 224 | *.appx 225 | *.appxbundle 226 | *.appxupload 227 | 228 | # Visual Studio cache files 229 | # files ending in .cache can be ignored 230 | *.[Cc]ache 231 | # but keep track of directories ending in .cache 232 | !?*.[Cc]ache/ 233 | 234 | # Others 235 | ClientBin/ 236 | ~$* 237 | *~ 238 | *.dbmdl 239 | *.dbproj.schemaview 240 | *.jfm 241 | *.pfx 242 | *.publishsettings 243 | orleans.codegen.cs 244 | 245 | # Including strong name files can present a security risk 246 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 247 | #*.snk 248 | 249 | # Since there are multiple workflows, uncomment next line to ignore bower_components 250 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 251 | #bower_components/ 252 | 253 | # RIA/Silverlight projects 254 | Generated_Code/ 255 | 256 | # Backup & report files from converting an old project file 257 | # to a newer Visual Studio version. Backup files are not needed, 258 | # because we have git ;-) 259 | _UpgradeReport_Files/ 260 | Backup*/ 261 | UpgradeLog*.XML 262 | UpgradeLog*.htm 263 | ServiceFabricBackup/ 264 | *.rptproj.bak 265 | 266 | # SQL Server files 267 | *.mdf 268 | *.ldf 269 | *.ndf 270 | 271 | # Business Intelligence projects 272 | *.rdl.data 273 | *.bim.layout 274 | *.bim_*.settings 275 | *.rptproj.rsuser 276 | *- [Bb]ackup.rdl 277 | *- [Bb]ackup ([0-9]).rdl 278 | *- [Bb]ackup ([0-9][0-9]).rdl 279 | 280 | # Microsoft Fakes 281 | FakesAssemblies/ 282 | 283 | # GhostDoc plugin setting file 284 | *.GhostDoc.xml 285 | 286 | # Node.js Tools for Visual Studio 287 | .ntvs_analysis.dat 288 | node_modules/ 289 | 290 | # Visual Studio 6 build log 291 | *.plg 292 | 293 | # Visual Studio 6 workspace options file 294 | *.opt 295 | 296 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 297 | *.vbw 298 | 299 | # Visual Studio LightSwitch build output 300 | **/*.HTMLClient/GeneratedArtifacts 301 | **/*.DesktopClient/GeneratedArtifacts 302 | **/*.DesktopClient/ModelManifest.xml 303 | **/*.Server/GeneratedArtifacts 304 | **/*.Server/ModelManifest.xml 305 | _Pvt_Extensions 306 | 307 | # Paket dependency manager 308 | .paket/paket.exe 309 | paket-files/ 310 | 311 | # FAKE - F# Make 312 | .fake/ 313 | 314 | # CodeRush personal settings 315 | .cr/personal 316 | 317 | # Python Tools for Visual Studio (PTVS) 318 | __pycache__/ 319 | *.pyc 320 | 321 | # Cake - Uncomment if you are using it 322 | # tools/** 323 | # !tools/packages.config 324 | 325 | # Tabs Studio 326 | *.tss 327 | 328 | # Telerik's JustMock configuration file 329 | *.jmconfig 330 | 331 | # BizTalk build output 332 | *.btp.cs 333 | *.btm.cs 334 | *.odx.cs 335 | *.xsd.cs 336 | 337 | # OpenCover UI analysis results 338 | OpenCover/ 339 | 340 | # Azure Stream Analytics local run output 341 | ASALocalRun/ 342 | 343 | # MSBuild Binary and Structured Log 344 | *.binlog 345 | 346 | # NVidia Nsight GPU debugger configuration file 347 | *.nvuser 348 | 349 | # MFractors (Xamarin productivity tool) working folder 350 | .mfractor/ 351 | 352 | # Local History for Visual Studio 353 | .localhistory/ 354 | 355 | # BeatPulse healthcheck temp database 356 | healthchecksdb 357 | 358 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 359 | MigrationBackup/ 360 | 361 | # Ionide (cross platform F# VS Code tools) working folder 362 | .ionide/ 363 | 364 | # Fody - auto-generated XML schema 365 | FodyWeavers.xsd 366 | 367 | ## 368 | ## Visual studio for Mac 369 | ## 370 | 371 | 372 | # globs 373 | Makefile.in 374 | *.userprefs 375 | *.usertasks 376 | config.make 377 | config.status 378 | aclocal.m4 379 | install-sh 380 | autom4te.cache/ 381 | *.tar.gz 382 | tarballs/ 383 | test-results/ 384 | 385 | # Mac bundle stuff 386 | *.dmg 387 | *.app 388 | 389 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 390 | # General 391 | .DS_Store 392 | .AppleDouble 393 | .LSOverride 394 | 395 | # Icon must end with two \r 396 | Icon 397 | 398 | 399 | # Thumbnails 400 | ._* 401 | 402 | # Files that might appear in the root of a volume 403 | .DocumentRevisions-V100 404 | .fseventsd 405 | .Spotlight-V100 406 | .TemporaryItems 407 | .Trashes 408 | .VolumeIcon.icns 409 | .com.apple.timemachine.donotpresent 410 | 411 | # Directories potentially created on remote AFP share 412 | .AppleDB 413 | .AppleDesktop 414 | Network Trash Folder 415 | Temporary Items 416 | .apdisk 417 | 418 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 419 | # Windows thumbnail cache files 420 | Thumbs.db 421 | ehthumbs.db 422 | ehthumbs_vista.db 423 | 424 | # Dump file 425 | *.stackdump 426 | 427 | # Folder config file 428 | [Dd]esktop.ini 429 | 430 | # Recycle Bin used on file shares 431 | $RECYCLE.BIN/ 432 | 433 | # Windows Installer files 434 | *.cab 435 | *.msi 436 | *.msix 437 | *.msm 438 | *.msp 439 | 440 | # Windows shortcuts 441 | *.lnk 442 | 443 | # JetBrains Rider 444 | .idea/ 445 | *.sln.iml 446 | 447 | ## 448 | ## Visual Studio Code 449 | ## 450 | .vscode/* 451 | !.vscode/settings.json 452 | !.vscode/tasks.json 453 | !.vscode/launch.json 454 | !.vscode/extensions.json 455 | -------------------------------------------------------------------------------- /AsyncProgramming.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31424.327 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA01ThreadVsTasks", "CA01ThreadVsTasks\CA01ThreadVsTasks.csproj", "{B06349C5-0740-4F48-B78F-75BCCDB80780}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA02TaskReturnsValue", "CA02TaskReturnsValue\CA02TaskReturnsValue.csproj", "{C8B4586F-E3EB-464E-8244-9844365D64BD}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA03LongRunningTask", "CA03LongRunningTask\CA03LongRunningTask.csproj", "{BBD5FA3C-504A-4DC0-A841-0EF82E080061}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA04ExceptionPropagation", "CA04ExceptionPropagation\CA04ExceptionPropagation.csproj", "{A54DC93D-3BA4-4A59-8307-970A99AB44C7}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA05TaskContinuation", "CA05TaskContinuation\CA05TaskContinuation.csproj", "{57113764-5350-4145-9736-1787ED5E14DB}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA06TaskDelay", "CA06TaskDelay\CA06TaskDelay.csproj", "{0998EA87-ACD5-4CA1-870E-09506E228CFD}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA07SyncVsAsync", "CA07SyncVsAsync\CA07SyncVsAsync.csproj", "{94E16AFE-C4D7-4E61-B559-9D432FDD7662}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA08AsyncFunctions", "CA08AsyncFunctions\CA08AsyncFunctions.csproj", "{A3B1B318-C6F3-4875-B5C4-7AC032F5055F}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CA09CancellationToken", "CA09CancellationToken\CA09CancellationToken.csproj", "{2910A7C0-470D-484E-8D81-5E6A61529408}" 23 | EndProject 24 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CA10ReportProgress", "CA10ReportProgress\CA10ReportProgress.csproj", "{D19D469B-5B85-4ED2-8AF2-185055B2D66E}" 25 | EndProject 26 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CA11TaskCombinators", "CA11TaskCombinators\CA11TaskCombinators.csproj", "{AF6DD5C4-38B4-4EC1-BDBC-3E031E5249F9}" 27 | EndProject 28 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CA12ConcurrencyAndParallelism", "CA12ConcurrencyAndParallelism\CA12ConcurrencyAndParallelism.csproj", "{40210400-C37F-4BB8-BBA9-8639A57F7DCC}" 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Release|Any CPU = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 36 | {B06349C5-0740-4F48-B78F-75BCCDB80780}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {B06349C5-0740-4F48-B78F-75BCCDB80780}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {B06349C5-0740-4F48-B78F-75BCCDB80780}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {B06349C5-0740-4F48-B78F-75BCCDB80780}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {C8B4586F-E3EB-464E-8244-9844365D64BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {C8B4586F-E3EB-464E-8244-9844365D64BD}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {C8B4586F-E3EB-464E-8244-9844365D64BD}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {C8B4586F-E3EB-464E-8244-9844365D64BD}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {BBD5FA3C-504A-4DC0-A841-0EF82E080061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {BBD5FA3C-504A-4DC0-A841-0EF82E080061}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {BBD5FA3C-504A-4DC0-A841-0EF82E080061}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {BBD5FA3C-504A-4DC0-A841-0EF82E080061}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {A54DC93D-3BA4-4A59-8307-970A99AB44C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {A54DC93D-3BA4-4A59-8307-970A99AB44C7}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {A54DC93D-3BA4-4A59-8307-970A99AB44C7}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {A54DC93D-3BA4-4A59-8307-970A99AB44C7}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {57113764-5350-4145-9736-1787ED5E14DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {57113764-5350-4145-9736-1787ED5E14DB}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {57113764-5350-4145-9736-1787ED5E14DB}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {57113764-5350-4145-9736-1787ED5E14DB}.Release|Any CPU.Build.0 = Release|Any CPU 56 | {0998EA87-ACD5-4CA1-870E-09506E228CFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 57 | {0998EA87-ACD5-4CA1-870E-09506E228CFD}.Debug|Any CPU.Build.0 = Debug|Any CPU 58 | {0998EA87-ACD5-4CA1-870E-09506E228CFD}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {0998EA87-ACD5-4CA1-870E-09506E228CFD}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {94E16AFE-C4D7-4E61-B559-9D432FDD7662}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 | {94E16AFE-C4D7-4E61-B559-9D432FDD7662}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 | {94E16AFE-C4D7-4E61-B559-9D432FDD7662}.Release|Any CPU.ActiveCfg = Release|Any CPU 63 | {94E16AFE-C4D7-4E61-B559-9D432FDD7662}.Release|Any CPU.Build.0 = Release|Any CPU 64 | {A3B1B318-C6F3-4875-B5C4-7AC032F5055F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {A3B1B318-C6F3-4875-B5C4-7AC032F5055F}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {A3B1B318-C6F3-4875-B5C4-7AC032F5055F}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 | {A3B1B318-C6F3-4875-B5C4-7AC032F5055F}.Release|Any CPU.Build.0 = Release|Any CPU 68 | {2910A7C0-470D-484E-8D81-5E6A61529408}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {2910A7C0-470D-484E-8D81-5E6A61529408}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {2910A7C0-470D-484E-8D81-5E6A61529408}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {2910A7C0-470D-484E-8D81-5E6A61529408}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {D19D469B-5B85-4ED2-8AF2-185055B2D66E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 73 | {D19D469B-5B85-4ED2-8AF2-185055B2D66E}.Debug|Any CPU.Build.0 = Debug|Any CPU 74 | {D19D469B-5B85-4ED2-8AF2-185055B2D66E}.Release|Any CPU.ActiveCfg = Release|Any CPU 75 | {D19D469B-5B85-4ED2-8AF2-185055B2D66E}.Release|Any CPU.Build.0 = Release|Any CPU 76 | {AF6DD5C4-38B4-4EC1-BDBC-3E031E5249F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {AF6DD5C4-38B4-4EC1-BDBC-3E031E5249F9}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {AF6DD5C4-38B4-4EC1-BDBC-3E031E5249F9}.Release|Any CPU.ActiveCfg = Release|Any CPU 79 | {AF6DD5C4-38B4-4EC1-BDBC-3E031E5249F9}.Release|Any CPU.Build.0 = Release|Any CPU 80 | {40210400-C37F-4BB8-BBA9-8639A57F7DCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 81 | {40210400-C37F-4BB8-BBA9-8639A57F7DCC}.Debug|Any CPU.Build.0 = Debug|Any CPU 82 | {40210400-C37F-4BB8-BBA9-8639A57F7DCC}.Release|Any CPU.ActiveCfg = Release|Any CPU 83 | {40210400-C37F-4BB8-BBA9-8639A57F7DCC}.Release|Any CPU.Build.0 = Release|Any CPU 84 | EndGlobalSection 85 | GlobalSection(SolutionProperties) = preSolution 86 | HideSolutionNode = FALSE 87 | EndGlobalSection 88 | GlobalSection(ExtensibilityGlobals) = postSolution 89 | SolutionGuid = {94B783EF-9DCE-4EAA-B192-F8DD153F9940} 90 | EndGlobalSection 91 | EndGlobal 92 | -------------------------------------------------------------------------------- /CA01ThreadVsTasks/CA01ThreadVsTasks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA01ThreadVsTasks/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA01ThreadVsTasks 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var th = new Thread(() => Display("Metigator using thread !!!")); 12 | th.Start(); 13 | th.Join(); 14 | 15 | Task.Run(() => Display("Metigator using task !!!")).Wait(); 16 | Console.ReadKey(); 17 | } 18 | 19 | static void Display(string message) 20 | { 21 | ShowThreadInfo(Thread.CurrentThread); 22 | Console.WriteLine(message); 23 | } 24 | 25 | private static void ShowThreadInfo(Thread th) 26 | { 27 | Console.WriteLine($"TID: {th.ManagedThreadId}, Pooled: {th.IsThreadPoolThread}, Background: {th.IsBackground}"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CA02TaskReturnsValue/CA02TaskReturnsValue.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA02TaskReturnsValue/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace CA02TaskReturnsValue 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Task task = Task.Run(GetCurrentDatetime); 11 | //Console.WriteLine(task.Result); // block thead until result is ready 12 | 13 | Console.WriteLine(task.GetAwaiter().GetResult()); 14 | Console.ReadKey(); 15 | } 16 | 17 | static DateTime GetCurrentDatetime() => DateTime.Now; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CA03LongRunningTask/CA03LongRunningTask.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA03LongRunningTask/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA03LongRunningTask 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var task = Task.Factory.StartNew(() => RunLongTask(), 12 | TaskCreationOptions.LongRunning); 13 | Console.ReadKey(); 14 | } 15 | 16 | static void RunLongTask() 17 | { 18 | Thread.Sleep(3000); 19 | ShowThreadInfo(Thread.CurrentThread); 20 | Console.WriteLine("Completed"); 21 | } 22 | static void ShowThreadInfo(Thread th) 23 | { 24 | Console.WriteLine($"TID: {th.ManagedThreadId}, Pooled: {th.IsThreadPoolThread}, Background: {th.IsBackground}"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CA04ExceptionPropagation/CA04ExceptionPropagation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA04ExceptionPropagation/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA04ExceptionPropagation 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | // -- 1 -- 12 | //try 13 | //{ 14 | // var th = new Thread(ThrowException); 15 | // th.Start(); 16 | // th.Join(); 17 | //} 18 | //catch 19 | //{ 20 | // Console.WriteLine("Exception is thrown!!"); 21 | //} 22 | 23 | // -- 2 -- 24 | //var th = new Thread(ThrowExceptionWithTryCatchBlock); 25 | //th.Start(); 26 | //th.Join(); 27 | 28 | // -- 3 -- 29 | 30 | try 31 | { 32 | Task.Run(ThrowException).Wait(); 33 | } 34 | catch 35 | { 36 | Console.WriteLine("Exception is thrown!!"); 37 | 38 | } 39 | Console.ReadKey(); 40 | } 41 | 42 | static void ThrowException() 43 | { 44 | throw new NullReferenceException(); 45 | } 46 | 47 | static void ThrowExceptionWithTryCatchBlock() 48 | { 49 | try 50 | { 51 | throw new NullReferenceException(); 52 | 53 | } 54 | catch 55 | { 56 | Console.WriteLine("Exception is thrown!!"); 57 | 58 | throw; 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /CA05TaskContinuation/CA05TaskContinuation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA05TaskContinuation/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace CA05TaskContinuation 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | // Console.WriteLine(CountPrimeNumberInARange(2, 2_000_000)); 11 | 12 | Task task = Task.Run(() => CountPrimeNumberInARange(2, 3_000_000)); 13 | // Console.WriteLine(task.Result); // bad it blocks the thead 14 | 15 | //Console.WriteLine("using awaiter, onComplete"); 16 | //var awaiter = task.GetAwaiter(); 17 | //awaiter.OnCompleted(() => { 18 | // Console.WriteLine(awaiter.GetResult()); // block the thread but task is completed 19 | //}); 20 | //Console.WriteLine("using task continuewith"); 21 | 22 | task.ContinueWith((x) => Console.WriteLine(x.Result)); 23 | Console.WriteLine("Metigator"); 24 | Console.ReadKey(); 25 | } 26 | 27 | static int CountPrimeNumberInARange(int lowerBound, int upperBound) 28 | { 29 | var count = 0; 30 | for (int i = lowerBound; i < upperBound; i++) 31 | { 32 | var j = 2; 33 | var isPrime = true; 34 | while(j <= (int)Math.Sqrt(i)) 35 | { 36 | if(i % j == 0) 37 | { 38 | isPrime = false; 39 | break; 40 | } 41 | ++j; 42 | } 43 | 44 | if (isPrime) 45 | ++count; 46 | } 47 | return count; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /CA06TaskDelay/CA06TaskDelay.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA06TaskDelay/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA06TaskDelay 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | DelayUsingTask(5000); 12 | Console.ReadKey(); 13 | } 14 | 15 | static void DelayUsingTask(int ms) 16 | { 17 | Task.Delay(ms).GetAwaiter().OnCompleted(() => { 18 | Console.WriteLine($"Completed after Task.Delay({ms})"); 19 | 20 | }); 21 | } 22 | 23 | static void SleepUsingThread(int ms) 24 | { 25 | Thread.Sleep(ms); 26 | Console.WriteLine($"Completed after Thread.Sleep({ms})"); 27 | 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CA07SyncVsAsync/CA07SyncVsAsync.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA07SyncVsAsync/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA07SyncVsAsync 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | ShowThreadInfo(Thread.CurrentThread, 11); 12 | CallSynchronous(); 13 | 14 | ShowThreadInfo(Thread.CurrentThread, 14); 15 | CallAsynchronous(); 16 | 17 | ShowThreadInfo(Thread.CurrentThread, 17); 18 | Console.ReadKey(); 19 | } 20 | 21 | static void CallSynchronous() 22 | { 23 | Thread.Sleep(4000); 24 | ShowThreadInfo(Thread.CurrentThread, 24); 25 | Task.Run(() => Console.WriteLine("++++++++++ Synchronous +++++++++++")).Wait(); 26 | } 27 | 28 | static void CallAsynchronous() 29 | { 30 | ShowThreadInfo(Thread.CurrentThread, 30); 31 | Task.Delay(4000).GetAwaiter().OnCompleted(() => { 32 | ShowThreadInfo(Thread.CurrentThread, 32); 33 | Console.WriteLine("++++++++++ Asynchronous +++++++++++"); 34 | }); 35 | } 36 | 37 | 38 | private static void ShowThreadInfo(Thread th, int line) 39 | { 40 | Console.WriteLine($"Line#: {line}, TID: {th.ManagedThreadId}, Pooled: {th.IsThreadPoolThread}, Background: {th.IsBackground}"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CA08AsyncFunctions/CA08AsyncFunctions.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA08AsyncFunctions/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net.Http; 3 | using System.Threading.Tasks; 4 | 5 | namespace CAAsyncFunctions 6 | { 7 | class Program 8 | { 9 | static async Task Main(string[] args) 10 | { 11 | // -- 1 -- 12 | //var task = Task.Run(() => ReadContent("https://www.youtube.com/c/Metigator")); 13 | //var awaiter = task.GetAwaiter(); 14 | //awaiter.OnCompleted(() => Console.WriteLine(awaiter.GetResult())); 15 | 16 | Console.WriteLine(await ReadContentAsync("https://www.youtube.com/c/Metigator")); 17 | Console.ReadKey(); 18 | } 19 | 20 | static Task ReadContent(string url) 21 | { 22 | var client = new HttpClient(); 23 | 24 | var task = client.GetStringAsync(url); 25 | 26 | return task; 27 | } 28 | 29 | static async Task ReadContentAsync(string url) 30 | { 31 | var client = new HttpClient(); 32 | 33 | var content = await client.GetStringAsync(url); 34 | 35 | return content; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /CA09CancellationToken/CA09CancellationToken.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA09CancellationToken/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace CA09CancellationToken 6 | { 7 | class Program 8 | { 9 | static async Task Main(string[] args) 10 | { 11 | CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); 12 | //await DoCheck01(cancellationTokenSource); 13 | //await DoCheck02(cancellationTokenSource); 14 | await DoCheck03(cancellationTokenSource); 15 | 16 | 17 | Console.ReadKey(); 18 | } 19 | 20 | static async Task DoCheck01(CancellationTokenSource cancellationTokenSource) 21 | { 22 | Task.Run(() => { 23 | var input = Console.ReadKey(); 24 | if(input.Key == ConsoleKey.Q) 25 | { 26 | cancellationTokenSource.Cancel(); 27 | Console.WriteLine("Task has been cancelled !!!"); 28 | } 29 | }); 30 | 31 | while(!cancellationTokenSource.Token.IsCancellationRequested) 32 | { 33 | Console.Write("Checking ..."); 34 | await Task.Delay(4000); 35 | Console.Write($" Completed on {DateTime.Now}"); 36 | Console.WriteLine(); 37 | } 38 | 39 | Console.WriteLine("Check was Terminated"); 40 | cancellationTokenSource.Dispose(); 41 | } 42 | 43 | static async Task DoCheck02(CancellationTokenSource cancellationTokenSource) 44 | { 45 | Task.Run(() => { 46 | var input = Console.ReadKey(); 47 | if (input.Key == ConsoleKey.Q) 48 | { 49 | cancellationTokenSource.Cancel(); 50 | Console.WriteLine("Task has been cancelled !!!"); 51 | } 52 | }); 53 | 54 | while (true) 55 | { 56 | Console.Write("Checking ..."); 57 | await Task.Delay(4000, cancellationTokenSource.Token); 58 | Console.Write($" Completed on {DateTime.Now}"); 59 | Console.WriteLine(); 60 | } 61 | 62 | Console.WriteLine("Check was Terminated"); 63 | cancellationTokenSource.Dispose(); 64 | } 65 | 66 | static async Task DoCheck03(CancellationTokenSource cancellationTokenSource) 67 | { 68 | Task.Run(() => { 69 | var input = Console.ReadKey(); 70 | if (input.Key == ConsoleKey.Q) 71 | { 72 | cancellationTokenSource.Cancel(); 73 | Console.WriteLine("Task has been cancelled !!!"); 74 | } 75 | }); 76 | 77 | try 78 | { 79 | while (true) 80 | { 81 | cancellationTokenSource.Token.ThrowIfCancellationRequested(); 82 | Console.Write("Checking ..."); 83 | await Task.Delay(4000); 84 | Console.Write($" Completed on {DateTime.Now}"); 85 | Console.WriteLine(); 86 | } 87 | } 88 | catch(Exception ex) 89 | { 90 | Console.WriteLine(ex.Message); 91 | } 92 | 93 | Console.WriteLine("Check was Terminated"); 94 | cancellationTokenSource.Dispose(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /CA10ReportProgress/CA10ReportProgress.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA10ReportProgress/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace CA10ReportProgress 5 | { 6 | class Program 7 | { 8 | static async Task Main(string[] args) 9 | { 10 | Action progress = (p) => { Console.Clear(); Console.WriteLine($"{p}%"); }; 11 | await Copy(progress); 12 | Console.ReadKey(); 13 | } 14 | 15 | static Task Copy(Action onProgressPercentChanged) 16 | { 17 | return Task.Run(() => { 18 | for (int i = 0; i <= 100; i++) 19 | { 20 | Task.Delay(50).Wait(); 21 | if (i % 10 == 0) 22 | onProgressPercentChanged(i); 23 | } 24 | }); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /CA11TaskCombinators/CA11TaskCombinators.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA11TaskCombinators/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace CA11TaskCombinators 5 | { 6 | class Program 7 | { 8 | static async Task Main(string[] args) 9 | { 10 | var has1000SubscriberTask = Task.Run(() => Has1000Subscriber()); 11 | var has4000ViewHoursTask = Task.Run(() => Has4000ViewHours()); 12 | Console.WriteLine("Using WhenAny()"); 13 | Console.WriteLine("---------------"); 14 | 15 | var any = await Task.WhenAny(has1000SubscriberTask, has4000ViewHoursTask); 16 | Console.WriteLine(any.Result); 17 | 18 | Console.WriteLine("Using WhenAll()"); 19 | Console.WriteLine("---------------"); 20 | 21 | var all = await Task.WhenAll(has1000SubscriberTask, has4000ViewHoursTask); 22 | foreach (var t in all) 23 | { 24 | Console.WriteLine(t); 25 | 26 | } 27 | Console.ReadKey(); 28 | } 29 | 30 | static Task Has1000Subscriber() 31 | { 32 | Task.Delay(4000).Wait(); 33 | return Task.FromResult("congratulation !! you have 1000 subscribers"); 34 | } 35 | 36 | static Task Has4000ViewHours() 37 | { 38 | Task.Delay(3000).Wait(); 39 | return Task.FromResult("congratulation !! you have 4000 view hours"); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /CA12ConcurrencyAndParallelism/CA12ConcurrencyAndParallelism.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CA12ConcurrencyAndParallelism/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | namespace CA12ConcurrencyAndParallelism 7 | { 8 | class Program 9 | { 10 | static async Task Main(string[] args) 11 | { 12 | var things = new List 13 | { 14 | new DailyDuty("Cleaning House"), 15 | new DailyDuty("Washing Dishes"), 16 | new DailyDuty("Doing Laundry"), 17 | new DailyDuty("Preparing Meals"), 18 | new DailyDuty("Checking Emails"), 19 | new DailyDuty("Cleaning House") 20 | }; 21 | 22 | //Console.WriteLine("Using Parallel Processing"); 23 | //await ProcessThingsInParallel(things); 24 | 25 | Console.WriteLine("Using Concurrent Processing"); 26 | await ProcessThingsInConcurrent(things); 27 | 28 | Console.ReadKey(); 29 | } 30 | 31 | static Task ProcessThingsInParallel(IEnumerable things) 32 | { 33 | Parallel.ForEach(things, thing => thing.Process()); 34 | return Task.CompletedTask; 35 | } 36 | 37 | static Task ProcessThingsInConcurrent(IEnumerable things) 38 | { 39 | foreach (var thing in things) 40 | { 41 | thing.Process(); 42 | } 43 | return Task.CompletedTask; 44 | } 45 | } 46 | 47 | class DailyDuty 48 | { 49 | public string title { get; private set; } 50 | 51 | public bool Processed { get; private set; } 52 | 53 | public DailyDuty(string title) 54 | { 55 | this.title = title; 56 | } 57 | 58 | public void Process() 59 | { 60 | Console.WriteLine($"TID: {Thread.CurrentThread.ManagedThreadId},"+ 61 | $"ProcessorId: {Thread.GetCurrentProcessorId()}"); 62 | Task.Delay(100).Wait(); 63 | this.Processed = true; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Metigator 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------