├── 01-AsyncDesktop
├── slides
│ ├── 03-CodeCompare.PNG
│ ├── 01-AsyncDesktopBad.PNG
│ └── 02-AsyncDesktopGood.PNG
├── after
│ ├── AsyncDesktopDemo
│ │ ├── AsyncDesktopDemo.csproj
│ │ ├── App.xaml
│ │ ├── App.xaml.cs
│ │ ├── MainWindow.xaml
│ │ └── MainWindow.xaml.cs
│ └── AsyncDesktopDemo.sln
├── before
│ ├── AsyncDesktopDemo
│ │ ├── AsyncDesktopDemo.csproj
│ │ ├── App.xaml
│ │ ├── App.xaml.cs
│ │ ├── MainWindow.xaml
│ │ └── MainWindow.xaml.cs
│ └── AsyncDesktopDemo.sln
└── README.md
├── 03-AsyncProgressDesktop
├── slides
│ ├── 01-Start.png
│ ├── 03-Done.png
│ └── 02-Progress.png
├── README.md
├── AsyncProgressDesktop
│ ├── App.xaml.cs
│ ├── AsyncProgressDesktop.csproj
│ ├── App.xaml
│ ├── Views
│ │ ├── MainWindow.xaml
│ │ └── MainWindow.xaml.cs
│ └── ViewModels
│ │ └── MainWindowViewModel.cs
└── AsyncProgressDesktop.sln
├── 02-AsyncWebAPI
├── slides
│ ├── 05-SyncCode-CPU-bound.PNG
│ ├── 06-AsyncCode-CPU-bound.PNG
│ ├── review
│ │ ├── 04-Parallel-Threads.PNG
│ │ ├── T01-Websurge-IO-bound.png
│ │ ├── 04-Parallel-Threads-Async.PNG
│ │ └── T01-Websurge-IO-bound-Sync.png
│ ├── 03-AsyncWebAPI-IO-bound-Code.PNG
│ ├── 01-AsyncWebAPI-IO-bound-Compare.PNG
│ ├── 02-AsyncWebAPI-IO-bound-CodeSync.PNG
│ ├── 04a-AsyncWebAPI-CPU-bound-Compare.PNG
│ └── 04b-AsyncWebAPI-CPU-bound-Compare.PNG
├── after
│ ├── AsyncWebAPIDemo
│ │ ├── appsettings.json
│ │ ├── AsyncWebAPIDemo.csproj
│ │ ├── appsettings.Development.json
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── Controllers
│ │ │ ├── CPUBoundController.cs
│ │ │ └── IOBoundController.cs
│ │ └── Startup.cs
│ └── AsyncWebAPIDemo.sln
├── before
│ ├── AsyncWebAPIDemo
│ │ ├── appsettings.json
│ │ ├── AsyncWebAPIDemo.csproj
│ │ ├── appsettings.Development.json
│ │ ├── Program.cs
│ │ ├── Properties
│ │ │ └── launchSettings.json
│ │ ├── Controllers
│ │ │ ├── CPUBoundController.cs
│ │ │ └── IOBoundController.cs
│ │ └── Startup.cs
│ └── AsyncWebAPIDemo.sln
└── README.md
├── 05-ParallelDemoDesktop
├── ParallelDemoDesktop
│ ├── lenna.png
│ ├── casabatllo.png
│ ├── sagradafamilia.png
│ ├── App.xaml.cs
│ ├── ParallelDemoDesktop.csproj
│ ├── App.xaml
│ ├── Views
│ │ ├── MainWindow.xaml
│ │ └── MainWindow.xaml.cs
│ └── ViewModels
│ │ └── MainWindowViewModel.cs
├── README.md
└── ParallelDemoDesktop.sln
├── 04-AsyncCancelDesktop
├── AsyncCancelDesktop
│ ├── App.xaml.cs
│ ├── AsyncCancelDesktop.csproj
│ ├── App.xaml
│ ├── Views
│ │ ├── MainWindow.xaml
│ │ └── MainWindow.xaml.cs
│ ├── CPULoad.cs
│ └── ViewModels
│ │ └── MainWindowViewModel.cs
├── README.md
└── AsyncCancelDesktop.sln
├── README.md
└── .gitignore
/01-AsyncDesktop/slides/03-CodeCompare.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/01-AsyncDesktop/slides/03-CodeCompare.PNG
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/slides/01-Start.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/03-AsyncProgressDesktop/slides/01-Start.png
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/slides/03-Done.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/03-AsyncProgressDesktop/slides/03-Done.png
--------------------------------------------------------------------------------
/01-AsyncDesktop/slides/01-AsyncDesktopBad.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/01-AsyncDesktop/slides/01-AsyncDesktopBad.PNG
--------------------------------------------------------------------------------
/01-AsyncDesktop/slides/02-AsyncDesktopGood.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/01-AsyncDesktop/slides/02-AsyncDesktopGood.PNG
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/slides/02-Progress.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/03-AsyncProgressDesktop/slides/02-Progress.png
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/05-SyncCode-CPU-bound.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/05-SyncCode-CPU-bound.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/06-AsyncCode-CPU-bound.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/06-AsyncCode-CPU-bound.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/review/04-Parallel-Threads.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/review/04-Parallel-Threads.PNG
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/lenna.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/05-ParallelDemoDesktop/ParallelDemoDesktop/lenna.png
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Warning"
5 | }
6 | },
7 | "AllowedHosts": "*"
8 | }
9 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/03-AsyncWebAPI-IO-bound-Code.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/03-AsyncWebAPI-IO-bound-Code.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/review/T01-Websurge-IO-bound.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/review/T01-Websurge-IO-bound.png
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/01-AsyncWebAPI-IO-bound-Compare.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/01-AsyncWebAPI-IO-bound-Compare.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/02-AsyncWebAPI-IO-bound-CodeSync.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/02-AsyncWebAPI-IO-bound-CodeSync.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/review/04-Parallel-Threads-Async.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/review/04-Parallel-Threads-Async.PNG
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/casabatllo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/05-ParallelDemoDesktop/ParallelDemoDesktop/casabatllo.png
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/04a-AsyncWebAPI-CPU-bound-Compare.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/04a-AsyncWebAPI-CPU-bound-Compare.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/04b-AsyncWebAPI-CPU-bound-Compare.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/04b-AsyncWebAPI-CPU-bound-Compare.PNG
--------------------------------------------------------------------------------
/02-AsyncWebAPI/slides/review/T01-Websurge-IO-bound-Sync.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/02-AsyncWebAPI/slides/review/T01-Websurge-IO-bound-Sync.png
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/sagradafamilia.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gerardo-lijs/Asynchronous-Programming-Samples/HEAD/05-ParallelDemoDesktop/ParallelDemoDesktop/sagradafamilia.png
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/AsyncWebAPIDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/AsyncWebAPIDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net5.0
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Debug",
5 | "System": "Information",
6 | "Microsoft": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo/AsyncDesktopDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net5.0-windows
6 | true
7 |
8 |
9 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/README.md:
--------------------------------------------------------------------------------
1 | # Parallel ForEach and Task.WhenAny demo application
2 | Demo soure code of a simple WPF Desktop Application to demonstrate how you can use Task Parallel Library ForEach for CPU-bound operations and how you can use Task.WhenAny for IO-bound operations
3 |
4 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo/AsyncDesktopDemo.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net5.0-windows
6 | true
7 |
8 |
9 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/README.md:
--------------------------------------------------------------------------------
1 | # Responsive Progress WPF application demo
2 | Demo source code of a WPF application to demonstrate responsive UI with a ProgressRing
3 |
4 | ## Start
5 | 
6 |
7 | ## Progress
8 | 
9 |
10 | ## Done
11 | 
12 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace AsyncCancelDesktop
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace ParallelDemoDesktop
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace AsyncProgressDesktop
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace AsyncDesktopDemo
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace AsyncDesktopDemo
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/README.md:
--------------------------------------------------------------------------------
1 | # Responsive WPF application demo
2 | Demo source code of a WPF application to demonstrate responsive UI and the beneficts of using async
3 |
4 | ## Synchronous code
5 | 
6 | This is bad! Application is not responding
7 |
8 | ## Asynchronous code
9 | 
10 | This is good! Application still responsive with 100% CPU use
11 |
12 | ## Code compare
13 | 
14 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/AsyncCancelDesktop.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net5.0-windows
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/AsyncProgressDesktop.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net5.0-windows
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Hosting;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.Hosting;
8 | using Microsoft.Extensions.Logging;
9 |
10 | namespace AsyncWebAPIDemo
11 | {
12 | public class Program
13 | {
14 | public static void Main(string[] args)
15 | {
16 | CreateHostBuilder(args).Build().Run();
17 | }
18 |
19 | public static IHostBuilder CreateHostBuilder(string[] args) =>
20 | Host.CreateDefaultBuilder(args)
21 | .ConfigureWebHostDefaults(webBuilder =>
22 | {
23 | webBuilder.UseStartup();
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Hosting;
6 | using Microsoft.Extensions.Configuration;
7 | using Microsoft.Extensions.Hosting;
8 | using Microsoft.Extensions.Logging;
9 |
10 | namespace AsyncWebAPIDemo
11 | {
12 | public class Program
13 | {
14 | public static void Main(string[] args)
15 | {
16 | CreateHostBuilder(args).Build().Run();
17 | }
18 |
19 | public static IHostBuilder CreateHostBuilder(string[] args) =>
20 | Host.CreateDefaultBuilder(args)
21 | .ConfigureWebHostDefaults(webBuilder =>
22 | {
23 | webBuilder.UseStartup();
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/README.md:
--------------------------------------------------------------------------------
1 | # Cancellation in a WPF application demo
2 | Demo soure code to demonstrate the simple use of CancellationToken
3 |
4 | In this demo we need to cancel a ParallelEnumerable (TPL) so we use the .WithCancellation extensions to pass the CancellationToken
5 |
6 | ```csharp
7 | private async Task CalculatePrimeNumbersImpl()
8 | {
9 | _ctsCalculatePrimeNumbers = new CancellationTokenSource();
10 |
11 | try
12 | {
13 | int primesCount = await Task.Run(() =>
14 | ParallelEnumerable.Range(StartNumber, EndNumber).WithCancellation(_ctsCalculatePrimeNumbers.Token).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
15 |
16 | ResultText = $"{primesCount} prime numbers between {StartNumber} and {EndNumber}";
17 | }
18 | catch (OperationCanceledException)
19 | {
20 | ResultText = "Cancelled";
21 | }
22 | }
23 | ```
24 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:60636",
8 | "sslPort": 0
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "api/cpubound?start=1&end=10000000",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "AsyncWebAPIDemo": {
21 | "commandName": "Project",
22 | "launchBrowser": true,
23 | "launchUrl": "api/cpubound?start=1&end=10000000",
24 | "applicationUrl": "http://localhost:5001",
25 | "environmentVariables": {
26 | "ASPNETCORE_ENVIRONMENT": "Development"
27 | }
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "iisSettings": {
4 | "windowsAuthentication": false,
5 | "anonymousAuthentication": true,
6 | "iisExpress": {
7 | "applicationUrl": "http://localhost:60635",
8 | "sslPort": 0
9 | }
10 | },
11 | "profiles": {
12 | "IIS Express": {
13 | "commandName": "IISExpress",
14 | "launchBrowser": true,
15 | "launchUrl": "api/cpubound?start=1&end=10000000",
16 | "environmentVariables": {
17 | "ASPNETCORE_ENVIRONMENT": "Development"
18 | }
19 | },
20 | "AsyncWebAPIDemo": {
21 | "commandName": "Project",
22 | "launchBrowser": true,
23 | "launchUrl": "api/cpubound?start=1&end=10000000",
24 | "applicationUrl": "http://localhost:5000",
25 | "environmentVariables": {
26 | "ASPNETCORE_ENVIRONMENT": "Development"
27 | }
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/Controllers/CPUBoundController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 |
8 | namespace AsyncWebAPIDemo.Controllers
9 | {
10 | [Route("api/[controller]")]
11 | [ApiController]
12 | public class CPUBoundController : ControllerBase
13 | {
14 | private int GetPrimesCount(int start, int count)
15 | {
16 | return ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0));
17 | }
18 |
19 | // GET api/cpubound
20 | // .\bombardier.exe "http://localhost:60635/api/cpubound?start=1&end=1000000" -n 20 -t 100s
21 | [HttpGet]
22 | public ActionResult Get([FromQuery] int start, [FromQuery] int end)
23 | {
24 | return GetPrimesCount(start, end);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 | 1
13 |
14 | 50000000
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 | 1
13 |
14 | 50000000
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/ParallelDemoDesktop.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | WinExe
5 | net5.0-windows
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 | PreserveNewest
19 |
20 |
21 | PreserveNewest
22 |
23 |
24 | PreserveNewest
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/Controllers/CPUBoundController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 |
8 | namespace AsyncWebAPIDemo.Controllers
9 | {
10 | [Route("api/[controller]")]
11 | [ApiController]
12 | public class CPUBoundController : ControllerBase
13 | {
14 | private Task GetPrimesCountAsync(int start, int count)
15 | {
16 | return Task.Run(() =>
17 | ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
18 | }
19 |
20 | // GET api/cpubound
21 | // .\bombardier.exe "http://localhost:60636/api/cpubound?start=1&end=1000000" -n 20 -t 100s
22 | [HttpGet]
23 | public async Task> Get([FromQuery] int start, [FromQuery] int end)
24 | {
25 | return await GetPrimesCountAsync(start, end);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncCancelDesktop", "AsyncCancelDesktop\AsyncCancelDesktop.csproj", "{3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7EE287A5-1681-4494-88D6-CC16068FC4A9}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelDemoDesktop", "ParallelDemoDesktop\ParallelDemoDesktop.csproj", "{3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7EE287A5-1681-4494-88D6-CC16068FC4A9}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncWebAPIDemo", "AsyncWebAPIDemo\AsyncWebAPIDemo.csproj", "{EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {5608948F-1E08-4F1D-9617-C7700CDE1897}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncWebAPIDemo", "AsyncWebAPIDemo\AsyncWebAPIDemo.csproj", "{EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {EA4AF0C1-7169-4030-9B49-A6D516AFD3FC}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {5608948F-1E08-4F1D-9617-C7700CDE1897}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncProgressDesktop", "AsyncProgressDesktop\AsyncProgressDesktop.csproj", "{3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7EE287A5-1681-4494-88D6-CC16068FC4A9}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncDesktopDemo", "AsyncDesktopDemo\AsyncDesktopDemo.csproj", "{3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7EE287A5-1681-4494-88D6-CC16068FC4A9}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.539
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncDesktopDemo", "AsyncDesktopDemo\AsyncDesktopDemo.csproj", "{3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {3D40C90F-BEB6-4435-8A07-CA05CCAEEC52}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {7EE287A5-1681-4494-88D6-CC16068FC4A9}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/README.md:
--------------------------------------------------------------------------------
1 | # Scalability and performance in web applications demo
2 | Demo source code of two Web API to demonstrate performance of using async with IO-bound and CPU-bound operations
3 |
4 | # Web API running IO-bound operation
5 |
6 | ## Synchronous code
7 | 
8 |
9 | ## Asynchronous code
10 | 
11 |
12 | ## Performance compare
13 | 
14 |
15 | > Using async/await makes it approximately ten times better!
16 |
17 | # Web API running IO-bound operation
18 |
19 | ## Synchronous code
20 | 
21 |
22 | ## Asynchronous code
23 | 
24 |
25 | ## Performance compare
26 | 
27 | Synchronous CPU-bound
28 |
29 | 
30 | Asynchronous CPU-bound
31 |
32 | > Using async/await makes it worse in this case!
33 |
34 | ## Summary
35 |
36 | * Use async with IO-bound operations whenever possible
37 | * Don’t block!
38 | * Don’t use async for expensive CPU-bound operations
39 | * Use benchmarking tools such as Bombardier (https://github.com/codesenberg/bombardier)
40 |
41 | I still need more performance, what can I do?
42 | * Scale up
43 | * Scale out whole application using Docker/Kubernetes
44 | * Scale out only expensive CPU-bound operations to Azure Functions
45 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Asynchronous programming in .NET samples
2 |
3 | * [Responsive WPF application demo](01-AsyncDesktop)
4 | Demo source code of a WPF application to demonstrate responsive UI and the beneficts of using async
5 | * [Scalability and performance in web applications demo](02-AsyncWebAPI)
6 | Demo source code of two Web API to demonstrate performance of using async with IO-bound and CPU-bound operations
7 | * [Progress in a WPF application demo](03-AsyncProgressDesktop)
8 | Demo soure code to demonstrate the simple use of a ProgressRing
9 | * [Cancellation in a WPF application demo](04-AsyncCancelDesktop)
10 | Demo soure code to demonstrate the simple use of CancellationToken
11 | * [Parallel ForEach and Task.WhenAny demo application](05-ParallelDemoDesktop)
12 | Demo soure code of a simple WPF Desktop Application to demonstrate how you can use Task Parallel Library ForEach for CPU-bound operations and how you can use Task.WhenAny for IO-bound operations
13 |
14 | ## Notes
15 | * In WPF desktop sample I didn't use MVVM to have a minimalistic example
16 | * In Progress, Cancellation and Parallel samples source code I used [ReactiveUI](https://reactiveui.net/) as MVVM framework, [MahApps](https://mahapps.com/) and [OpenCVSharp](https://github.com/shimat/opencvsharp) for simulating a CPU-bound expensive operation. If you are interested in this projects it's worth checkin it out since they are a minimalistic example and easy to see how they work.
17 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/before/AsyncDesktopDemo/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Data;
9 | using System.Windows.Documents;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 | using System.Windows.Media.Imaging;
13 | using System.Windows.Navigation;
14 | using System.Windows.Shapes;
15 |
16 | namespace AsyncDesktopDemo
17 | {
18 | ///
19 | /// Interaction logic for MainWindow.xaml
20 | ///
21 | public partial class MainWindow : Window
22 | {
23 | public MainWindow()
24 | {
25 | InitializeComponent();
26 | }
27 |
28 | private void Button_Click(object sender, RoutedEventArgs e)
29 | {
30 | int.TryParse(StartNumberTextBox.Text, out int start);
31 | int.TryParse(EndNumberTextBox.Text, out int end);
32 | if (start == 0 || end == 0) return;
33 |
34 | ResultTextBlock.Text = "";
35 | int result = GetPrimesCount(start, end);
36 | ResultTextBlock.Text = $"{result} prime numbers between {start} and {end}";
37 | }
38 |
39 | private int GetPrimesCount(int start, int count)
40 | {
41 | return ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0));
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/Views/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/01-AsyncDesktop/after/AsyncDesktopDemo/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Data;
9 | using System.Windows.Documents;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 | using System.Windows.Media.Imaging;
13 | using System.Windows.Navigation;
14 | using System.Windows.Shapes;
15 |
16 | namespace AsyncDesktopDemo
17 | {
18 | ///
19 | /// Interaction logic for MainWindow.xaml
20 | ///
21 | public partial class MainWindow : Window
22 | {
23 | public MainWindow()
24 | {
25 | InitializeComponent();
26 | }
27 |
28 | private async void Button_Click(object sender, RoutedEventArgs e)
29 | {
30 | int.TryParse(StartNumberTextBox.Text, out int start);
31 | int.TryParse(EndNumberTextBox.Text, out int end);
32 | if (start == 0 || end == 0) return;
33 |
34 | ResultTextBlock.Text = "";
35 | int result = await GetPrimesCountAsync(start, end);
36 | ResultTextBlock.Text = $"{result} prime numbers between {start} and {end}";
37 | }
38 |
39 | private Task GetPrimesCountAsync(int start, int count)
40 | {
41 | return Task.Run(() =>
42 | ParallelEnumerable.Range(start, count).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/Views/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/Controllers/IOBoundController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 |
8 | namespace AsyncWebAPIDemo.Controllers
9 | {
10 | [Route("api/[controller]")]
11 | [ApiController]
12 | public class IOBoundController : ControllerBase
13 | {
14 | private string GetHtml()
15 | {
16 | var client = new System.Net.WebClient();
17 | string response = client.DownloadString("https://www.dotnetfoundation.org");
18 | return response;
19 | }
20 |
21 | // GET api/iobound
22 | // .\bombardier.exe "http://localhost:60635/api/iobound" -n 20 -t 100s
23 | [HttpGet]
24 | public ActionResult Get()
25 | {
26 | var webContent = GetHtml();
27 | return webContent.Length;
28 | }
29 |
30 | // GET api/iobound/5
31 | [HttpGet("{id}")]
32 | public ActionResult Get(int id)
33 | {
34 | return "value";
35 | }
36 |
37 | // POST api/iobound
38 | [HttpPost]
39 | public void Post([FromBody] string value)
40 | {
41 | }
42 |
43 | // PUT api/iobound/5
44 | [HttpPut("{id}")]
45 | public void Put(int id, [FromBody] string value)
46 | {
47 | }
48 |
49 | // DELETE api/iobound/5
50 | [HttpDelete("{id}")]
51 | public void Delete(int id)
52 | {
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/Controllers/IOBoundController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 | using Microsoft.AspNetCore.Mvc;
7 |
8 | namespace AsyncWebAPIDemo.Controllers
9 | {
10 | [Route("api/[controller]")]
11 | [ApiController]
12 | public class IOBoundController : ControllerBase
13 | {
14 | private async Task GetHtmlAsync()
15 | {
16 | var client = new System.Net.Http.HttpClient();
17 | string response = await client.GetStringAsync("https://www.dotnetfoundation.org");
18 | return response;
19 | }
20 |
21 | // GET api/iobound
22 | // .\bombardier.exe "http://localhost:60636/api/iobound" -n 20 -t 100s
23 | [HttpGet]
24 | public async Task> Get()
25 | {
26 | var webContent = await GetHtmlAsync();
27 | return webContent.Length;
28 | }
29 |
30 | // GET api/iobound/5
31 | [HttpGet("{id}")]
32 | public ActionResult Get(int id)
33 | {
34 | return "value";
35 | }
36 |
37 | // POST api/iobound
38 | [HttpPost]
39 | public void Post([FromBody] string value)
40 | {
41 | }
42 |
43 | // PUT api/iobound/5
44 | [HttpPut("{id}")]
45 | public void Put(int id, [FromBody] string value)
46 | {
47 | }
48 |
49 | // DELETE api/iobound/5
50 | [HttpDelete("{id}")]
51 | public void Delete(int id)
52 | {
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/Views/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/Views/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using AsyncProgressDesktop.ViewModels;
2 | using ReactiveUI;
3 | using System.Reactive.Disposables;
4 |
5 | namespace AsyncProgressDesktop.Views
6 | {
7 | public partial class MainWindow : ReactiveWindow
8 | {
9 | public MainWindow()
10 | {
11 | InitializeComponent();
12 | ViewModel = new MainWindowViewModel();
13 | this.WhenActivated(disposableRegistration =>
14 | {
15 | // Values
16 | this.Bind(ViewModel, viewModel => viewModel.StartNumber, view => view.StartNumberNumericUpDown.Value, value => value, value => (int)value).DisposeWith(disposableRegistration);
17 | this.Bind(ViewModel, viewModel => viewModel.EndNumber, view => view.EndNumberNumericUpDown.Value, value => value, value => (int)value).DisposeWith(disposableRegistration);
18 | this.OneWayBind(ViewModel, viewModel => viewModel.ResultText, view => view.ResultTextBlock.Text).DisposeWith(disposableRegistration);
19 |
20 | // Commands
21 | this.BindCommand(ViewModel, viewModel => viewModel.CalculatePrimeNumbers, view => view.CalculatePrimeNumbersButton).DisposeWith(disposableRegistration);
22 |
23 | // Progress
24 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.CalculatingProgress.Visibility)
25 | .DisposeWith(disposableRegistration);
26 |
27 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.ResultTextBlock.Visibility,
28 | value => value ? System.Windows.Visibility.Collapsed: System.Windows.Visibility.Visible)
29 | .DisposeWith(disposableRegistration);
30 | });
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/after/AsyncWebAPIDemo/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Hosting;
7 | using Microsoft.AspNetCore.HttpsPolicy;
8 | using Microsoft.AspNetCore.Mvc;
9 | using Microsoft.Extensions.Configuration;
10 | using Microsoft.Extensions.DependencyInjection;
11 | using Microsoft.Extensions.Hosting;
12 | using Microsoft.Extensions.Logging;
13 |
14 | namespace AsyncWebAPIDemo
15 | {
16 | public class Startup
17 | {
18 | public Startup(IConfiguration configuration)
19 | {
20 | Configuration = configuration;
21 | }
22 |
23 | public IConfiguration Configuration { get; }
24 |
25 | // This method gets called by the runtime. Use this method to add services to the container.
26 | public void ConfigureServices(IServiceCollection services)
27 | {
28 | services.AddControllers();
29 | }
30 |
31 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
33 | {
34 | if (env.IsDevelopment())
35 | {
36 | app.UseDeveloperExceptionPage();
37 | }
38 | else
39 | {
40 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
41 | app.UseHsts();
42 | }
43 |
44 | app.UseHttpsRedirection();
45 |
46 | app.UseRouting();
47 |
48 | app.UseAuthorization();
49 |
50 | app.UseEndpoints(endpoints =>
51 | {
52 | endpoints.MapControllers();
53 | });
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/02-AsyncWebAPI/before/AsyncWebAPIDemo/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using Microsoft.AspNetCore.Builder;
6 | using Microsoft.AspNetCore.Hosting;
7 | using Microsoft.AspNetCore.HttpsPolicy;
8 | using Microsoft.AspNetCore.Mvc;
9 | using Microsoft.Extensions.Configuration;
10 | using Microsoft.Extensions.DependencyInjection;
11 | using Microsoft.Extensions.Hosting;
12 | using Microsoft.Extensions.Logging;
13 |
14 | namespace AsyncWebAPIDemo
15 | {
16 | public class Startup
17 | {
18 | public Startup(IConfiguration configuration)
19 | {
20 | Configuration = configuration;
21 | }
22 |
23 | public IConfiguration Configuration { get; }
24 |
25 | // This method gets called by the runtime. Use this method to add services to the container.
26 | public void ConfigureServices(IServiceCollection services)
27 | {
28 | services.AddControllers();
29 | }
30 |
31 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
32 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
33 | {
34 | if (env.IsDevelopment())
35 | {
36 | app.UseDeveloperExceptionPage();
37 | }
38 | else
39 | {
40 | // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
41 | app.UseHsts();
42 | }
43 |
44 | app.UseHttpsRedirection();
45 |
46 | app.UseRouting();
47 |
48 | app.UseAuthorization();
49 |
50 | app.UseEndpoints(endpoints =>
51 | {
52 | endpoints.MapControllers();
53 | });
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/03-AsyncProgressDesktop/AsyncProgressDesktop/ViewModels/MainWindowViewModel.cs:
--------------------------------------------------------------------------------
1 | using ReactiveUI;
2 | using System;
3 | using System.Linq;
4 | using System.Reactive;
5 | using System.Reactive.Linq;
6 | using System.Threading.Tasks;
7 |
8 | namespace AsyncProgressDesktop.ViewModels
9 | {
10 | public class MainWindowViewModel : ReactiveObject
11 | {
12 | // Commands
13 | public ReactiveCommand CalculatePrimeNumbers { get; }
14 |
15 | private int _StartNumber = 1;
16 | public int StartNumber
17 | {
18 | get => _StartNumber;
19 | set => this.RaiseAndSetIfChanged(ref _StartNumber, value);
20 | }
21 |
22 | private int _EndNumber = 5000000;
23 | public int EndNumber
24 | {
25 | get => _EndNumber;
26 | set => this.RaiseAndSetIfChanged(ref _EndNumber, value);
27 | }
28 |
29 | private string _ResultText;
30 | public string ResultText
31 | {
32 | get => _ResultText;
33 | private set => this.RaiseAndSetIfChanged(ref _ResultText, value);
34 | }
35 |
36 | private readonly ObservableAsPropertyHelper _IsCalculating;
37 | public bool IsCalculating => _IsCalculating.Value;
38 |
39 | public MainWindowViewModel()
40 | {
41 | // Create command
42 | CalculatePrimeNumbers = ReactiveCommand.CreateFromTask(CalculatePrimeNumbersImpl);
43 | CalculatePrimeNumbers.IsExecuting.ToProperty(this, x => x.IsCalculating, out _IsCalculating);
44 | }
45 |
46 | private async Task CalculatePrimeNumbersImpl()
47 | {
48 | int primesCount = await Task.Run(() =>
49 | ParallelEnumerable.Range(StartNumber, EndNumber).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
50 |
51 | ResultText = $"{primesCount} prime numbers between {StartNumber} and {EndNumber}";
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/CPULoad.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace LoadTesting
7 | {
8 | public static class CPU
9 | {
10 | ///
11 | /// Simulates an expensive CPU operation (all available cores) for the specified number of seconds.
12 | ///
13 | /// The number of seconds for which the CPU expensive operation is simulated
14 | public static async Task SimulateExpensiveMethodAsync(int seconds = 1)
15 | {
16 | using (var cts = new CancellationTokenSource(seconds * 1000))
17 | {
18 | try
19 | {
20 | await Task.Run(() =>
21 | ExpensiveMethod(cts.Token));
22 | }
23 | catch (OperationCanceledException)
24 | {
25 | // Expected exception in normal method flow
26 | }
27 | }
28 | }
29 |
30 | ///
31 | /// Simulates an expensive CPU operation (all available cores) for the specified number of seconds.
32 | ///
33 | /// The number of seconds for which the CPU expensive operation is simulated
34 | public static void SimulateExpensiveMethod(int seconds = 1)
35 | {
36 | using (var cts = new CancellationTokenSource(seconds * 1000))
37 | {
38 | try
39 | {
40 | ExpensiveMethod(cts.Token);
41 | }
42 | catch (OperationCanceledException)
43 | {
44 | // Expected exception in normal method flow
45 | }
46 | }
47 | }
48 |
49 | private static void ExpensiveMethod(CancellationToken cancellationToken) =>
50 | ParallelEnumerable.Range(1, int.MaxValue).WithCancellation(cancellationToken).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0));
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/Views/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using AsyncCancelDesktop.ViewModels;
2 | using ReactiveUI;
3 | using System.Reactive.Disposables;
4 |
5 | namespace AsyncCancelDesktop.Views
6 | {
7 | public partial class MainWindow : ReactiveWindow
8 | {
9 | public MainWindow()
10 | {
11 | InitializeComponent();
12 | ViewModel = new MainWindowViewModel();
13 | this.WhenActivated(disposableRegistration =>
14 | {
15 | // Values
16 | this.Bind(ViewModel, viewModel => viewModel.StartNumber, view => view.StartNumberNumericUpDown.Value, value => value, value => (int)value).DisposeWith(disposableRegistration);
17 | this.Bind(ViewModel, viewModel => viewModel.EndNumber, view => view.EndNumberNumericUpDown.Value, value => value, value => (int)value).DisposeWith(disposableRegistration);
18 | this.OneWayBind(ViewModel, viewModel => viewModel.ResultText, view => view.ResultTextBlock.Text).DisposeWith(disposableRegistration);
19 |
20 | // Commands
21 | this.BindCommand(ViewModel, viewModel => viewModel.CalculatePrimeNumbers, view => view.CalculatePrimeNumbersButton).DisposeWith(disposableRegistration);
22 |
23 | // Progress
24 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.CalculatingProgress.Visibility)
25 | .DisposeWith(disposableRegistration);
26 |
27 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.ResultTextBlock.Visibility,
28 | value => value ? System.Windows.Visibility.Collapsed: System.Windows.Visibility.Visible)
29 | .DisposeWith(disposableRegistration);
30 |
31 | // Cancel
32 | this.BindCommand(ViewModel, viewModel => viewModel.CalculateCancel, view => view.CancelButton).DisposeWith(disposableRegistration);
33 |
34 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.CancelButton.Visibility)
35 | .DisposeWith(disposableRegistration);
36 | });
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/Views/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using ParallelDemoDesktop.ViewModels;
2 | using ReactiveUI;
3 | using System.Reactive.Disposables;
4 |
5 | namespace ParallelDemoDesktop.Views
6 | {
7 | public partial class MainWindow : ReactiveWindow
8 | {
9 | public MainWindow()
10 | {
11 | InitializeComponent();
12 | ViewModel = new MainWindowViewModel();
13 | this.WhenActivated(disposableRegistration =>
14 | {
15 | this.WhenAnyValue(viewModel => viewModel.ViewModel).BindTo(this, view => view.DataContext).DisposeWith(disposableRegistration);
16 |
17 | // Values
18 | this.OneWayBind(ViewModel, viewModel => viewModel.ResultText, view => view.ResultTextBlock.Text).DisposeWith(disposableRegistration);
19 |
20 | // Commands
21 | this.BindCommand(ViewModel, viewModel => viewModel.ProcessImages, view => view.ProcessImagesButton).DisposeWith(disposableRegistration);
22 | this.BindCommand(ViewModel, viewModel => viewModel.ProcessImagesParallel, view => view.ProcessImagesParallelButton).DisposeWith(disposableRegistration);
23 |
24 | this.BindCommand(ViewModel, viewModel => viewModel.ProcessWeb, view => view.ProcessWebButton).DisposeWith(disposableRegistration);
25 | this.BindCommand(ViewModel, viewModel => viewModel.ProcessWebParallel, view => view.ProcessWebParallelButton).DisposeWith(disposableRegistration);
26 |
27 | // Progress
28 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.CalculatingProgress.Visibility)
29 | .DisposeWith(disposableRegistration);
30 |
31 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.ResultTextBlock.Visibility,
32 | value => value ? System.Windows.Visibility.Collapsed: System.Windows.Visibility.Visible)
33 | .DisposeWith(disposableRegistration);
34 |
35 | // Cancel
36 | this.BindCommand(ViewModel, viewModel => viewModel.CancelProcess, view => view.CancelButton).DisposeWith(disposableRegistration);
37 |
38 | this.OneWayBind(ViewModel, viewModel => viewModel.IsCalculating, view => view.CancelButton.Visibility)
39 | .DisposeWith(disposableRegistration);
40 | });
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/04-AsyncCancelDesktop/AsyncCancelDesktop/ViewModels/MainWindowViewModel.cs:
--------------------------------------------------------------------------------
1 | using ReactiveUI;
2 | using System;
3 | using System.Linq;
4 | using System.Reactive;
5 | using System.Reactive.Linq;
6 | using System.Threading;
7 | using System.Threading.Tasks;
8 |
9 | namespace AsyncCancelDesktop.ViewModels
10 | {
11 | public class MainWindowViewModel : ReactiveObject
12 | {
13 | // Commands
14 | public ReactiveCommand CalculatePrimeNumbers { get; }
15 |
16 | // Cancel
17 | public ReactiveCommand CalculateCancel { get; }
18 | private CancellationTokenSource _ctsCalculatePrimeNumbers { get; set; }
19 |
20 | private int _StartNumber = 1;
21 | public int StartNumber
22 | {
23 | get => _StartNumber;
24 | set => this.RaiseAndSetIfChanged(ref _StartNumber, value);
25 | }
26 |
27 | private int _EndNumber = 5000000;
28 | public int EndNumber
29 | {
30 | get => _EndNumber;
31 | set => this.RaiseAndSetIfChanged(ref _EndNumber, value);
32 | }
33 |
34 | private string _ResultText;
35 | public string ResultText
36 | {
37 | get => _ResultText;
38 | private set => this.RaiseAndSetIfChanged(ref _ResultText, value);
39 | }
40 |
41 | private readonly ObservableAsPropertyHelper _IsCalculating;
42 | public bool IsCalculating => _IsCalculating.Value;
43 |
44 | public MainWindowViewModel()
45 | {
46 | // Create command
47 | CalculatePrimeNumbers = ReactiveCommand.CreateFromTask(CalculatePrimeNumbersImpl);
48 | CalculatePrimeNumbers.IsExecuting.ToProperty(this, x => x.IsCalculating, out _IsCalculating);
49 |
50 | // TODO: Program another command with this CPU Load code. Add CancellationToken
51 | //await LoadTesting.CPU.SimulateExpensiveMethodAsync(15);
52 |
53 | // Cancel command
54 | CalculateCancel = ReactiveCommand.Create(() => _ctsCalculatePrimeNumbers?.Cancel(true));
55 | }
56 |
57 | private async Task CalculatePrimeNumbersImpl()
58 | {
59 | _ctsCalculatePrimeNumbers = new CancellationTokenSource();
60 |
61 | try
62 | {
63 | int primesCount = await Task.Run(() =>
64 | ParallelEnumerable.Range(StartNumber, EndNumber).WithCancellation(_ctsCalculatePrimeNumbers.Token).Count(n => Enumerable.Range(2, (int)Math.Sqrt(n) - 1).All(i => n % i > 0)));
65 |
66 | ResultText = $"{primesCount} prime numbers between {StartNumber} and {EndNumber}";
67 | }
68 | catch (OperationCanceledException)
69 | {
70 | ResultText = "Cancelled";
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | bld/
21 | [Bb]in/
22 | [Oo]bj/
23 | [Ll]og/
24 |
25 | # Visual Studio 2015 cache/options directory
26 | .vs/
27 | # Uncomment if you have tasks that create the project's static files in wwwroot
28 | #wwwroot/
29 |
30 | # MSTest test Results
31 | [Tt]est[Rr]esult*/
32 | [Bb]uild[Ll]og.*
33 |
34 | # NUNIT
35 | *.VisualState.xml
36 | TestResult.xml
37 |
38 | # Build Results of an ATL Project
39 | [Dd]ebugPS/
40 | [Rr]eleasePS/
41 | dlldata.c
42 |
43 | # DNX
44 | project.lock.json
45 | project.fragment.lock.json
46 | artifacts/
47 |
48 | *_i.c
49 | *_p.c
50 | *_i.h
51 | *.ilk
52 | *.meta
53 | *.obj
54 | *.pch
55 | *.pdb
56 | *.pgc
57 | *.pgd
58 | *.rsp
59 | *.sbr
60 | *.tlb
61 | *.tli
62 | *.tlh
63 | *.tmp
64 | *.tmp_proj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Chutzpah Test files
74 | _Chutzpah*
75 |
76 | # Visual C++ cache files
77 | ipch/
78 | *.aps
79 | *.ncb
80 | *.opendb
81 | *.opensdf
82 | *.sdf
83 | *.cachefile
84 | *.VC.db
85 | *.VC.VC.opendb
86 |
87 | # Visual Studio profiler
88 | *.psess
89 | *.vsp
90 | *.vspx
91 | *.sap
92 |
93 | # TFS 2012 Local Workspace
94 | $tf/
95 |
96 | # Guidance Automation Toolkit
97 | *.gpState
98 |
99 | # ReSharper is a .NET coding add-in
100 | _ReSharper*/
101 | *.[Rr]e[Ss]harper
102 | *.DotSettings.user
103 |
104 | # JustCode is a .NET coding add-in
105 | .JustCode
106 |
107 | # TeamCity is a build add-in
108 | _TeamCity*
109 |
110 | # DotCover is a Code Coverage Tool
111 | *.dotCover
112 |
113 | # NCrunch
114 | _NCrunch_*
115 | .*crunch*.local.xml
116 | nCrunchTemp_*
117 |
118 | # MightyMoose
119 | *.mm.*
120 | AutoTest.Net/
121 |
122 | # Web workbench (sass)
123 | .sass-cache/
124 |
125 | # Installshield output folder
126 | [Ee]xpress/
127 |
128 | # DocProject is a documentation generator add-in
129 | DocProject/buildhelp/
130 | DocProject/Help/*.HxT
131 | DocProject/Help/*.HxC
132 | DocProject/Help/*.hhc
133 | DocProject/Help/*.hhk
134 | DocProject/Help/*.hhp
135 | DocProject/Help/Html2
136 | DocProject/Help/html
137 |
138 | # Click-Once directory
139 | publish/
140 |
141 | # Publish Web Output
142 | *.[Pp]ublish.xml
143 | *.azurePubxml
144 | # TODO: Comment the next line if you want to checkin your web deploy settings
145 | # but database connection strings (with potential passwords) will be unencrypted
146 | #*.pubxml
147 | *.publishproj
148 |
149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
150 | # checkin your Azure Web App publish settings, but sensitive information contained
151 | # in these scripts will be unencrypted
152 | PublishScripts/
153 |
154 | # NuGet Packages
155 | *.nupkg
156 | # The packages folder can be ignored because of Package Restore
157 | **/packages/*
158 | # except build/, which is used as an MSBuild target.
159 | !**/packages/build/
160 | # Uncomment if necessary however generally it will be regenerated when needed
161 | #!**/packages/repositories.config
162 | # NuGet v3's project.json files produces more ignoreable files
163 | *.nuget.props
164 | *.nuget.targets
165 |
166 | # Microsoft Azure Build Output
167 | csx/
168 | *.build.csdef
169 |
170 | # Microsoft Azure Emulator
171 | ecf/
172 | rcf/
173 |
174 | # Windows Store app package directories and files
175 | AppPackages/
176 | BundleArtifacts/
177 | Package.StoreAssociation.xml
178 | _pkginfo.txt
179 |
180 | # Visual Studio cache files
181 | # files ending in .cache can be ignored
182 | *.[Cc]ache
183 | # but keep track of directories ending in .cache
184 | !*.[Cc]ache/
185 |
186 | # Others
187 | ClientBin/
188 | ~$*
189 | *~
190 | *.dbmdl
191 | *.dbproj.schemaview
192 | *.jfm
193 | *.pfx
194 | *.publishsettings
195 | node_modules/
196 | orleans.codegen.cs
197 |
198 | # Since there are multiple workflows, uncomment next line to ignore bower_components
199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
200 | #bower_components/
201 |
202 | # RIA/Silverlight projects
203 | Generated_Code/
204 |
205 | # Backup & report files from converting an old project file
206 | # to a newer Visual Studio version. Backup files are not needed,
207 | # because we have git ;-)
208 | _UpgradeReport_Files/
209 | Backup*/
210 | UpgradeLog*.XML
211 | UpgradeLog*.htm
212 |
213 | # SQL Server files
214 | *.mdf
215 | *.ldf
216 |
217 | # Business Intelligence projects
218 | *.rdl.data
219 | *.bim.layout
220 | *.bim_*.settings
221 |
222 | # Microsoft Fakes
223 | FakesAssemblies/
224 |
225 | # GhostDoc plugin setting file
226 | *.GhostDoc.xml
227 |
228 | # Node.js Tools for Visual Studio
229 | .ntvs_analysis.dat
230 |
231 | # Visual Studio 6 build log
232 | *.plg
233 |
234 | # Visual Studio 6 workspace options file
235 | *.opt
236 |
237 | # Visual Studio LightSwitch build output
238 | **/*.HTMLClient/GeneratedArtifacts
239 | **/*.DesktopClient/GeneratedArtifacts
240 | **/*.DesktopClient/ModelManifest.xml
241 | **/*.Server/GeneratedArtifacts
242 | **/*.Server/ModelManifest.xml
243 | _Pvt_Extensions
244 |
245 | # Paket dependency manager
246 | .paket/paket.exe
247 | paket-files/
248 |
249 | # FAKE - F# Make
250 | .fake/
251 |
252 | # JetBrains Rider
253 | .idea/
254 | *.sln.iml
255 |
256 | # CodeRush
257 | .cr/
258 |
259 | # Python Tools for Visual Studio (PTVS)
260 | __pycache__/
261 | *.pyc
--------------------------------------------------------------------------------
/05-ParallelDemoDesktop/ParallelDemoDesktop/ViewModels/MainWindowViewModel.cs:
--------------------------------------------------------------------------------
1 | using OpenCvSharp;
2 | using ReactiveUI;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Diagnostics;
6 | using System.Linq;
7 | using System.Reactive;
8 | using System.Reactive.Linq;
9 | using System.Threading;
10 | using System.Threading.Tasks;
11 |
12 | namespace ParallelDemoDesktop.ViewModels
13 | {
14 | public class MainWindowViewModel : ReactiveObject
15 | {
16 | // Commands
17 | public ReactiveCommand ProcessImages { get; }
18 | public ReactiveCommand ProcessImagesParallel { get; }
19 |
20 | public ReactiveCommand ProcessWeb { get; }
21 | public ReactiveCommand ProcessWebParallel { get; }
22 |
23 | // Cancel
24 | public ReactiveCommand CancelProcess { get; }
25 | private CancellationTokenSource _ctsCancel { get; set; }
26 |
27 | private string _ResultText;
28 | public string ResultText
29 | {
30 | get => _ResultText;
31 | private set => this.RaiseAndSetIfChanged(ref _ResultText, value);
32 | }
33 |
34 | private readonly ObservableAsPropertyHelper _IsCalculating;
35 | public bool IsCalculating => _IsCalculating.Value;
36 |
37 | public MainWindowViewModel()
38 | {
39 | // Create command
40 | ProcessImages = ReactiveCommand.CreateFromTask(ProcessImagesImpl);
41 | ProcessImagesParallel = ReactiveCommand.CreateFromTask(ProcessImagesParallelImpl);
42 |
43 | ProcessWeb = ReactiveCommand.CreateFromTask(ProcessWebImpl);
44 | ProcessWebParallel = ReactiveCommand.CreateFromTask(ProcessWebParallelImpl);
45 |
46 | // Progress
47 | _IsCalculating = this.WhenAnyObservable(x => x.ProcessImages.IsExecuting, x => x.ProcessImagesParallel.IsExecuting,
48 | x => x.ProcessWeb.IsExecuting, x => x.ProcessWebParallel.IsExecuting,
49 | (processImages, processImagesParallel, processWeb, processWebParallel) => processImages || processImagesParallel || processWeb || processWebParallel)
50 | .ToProperty(this, x => x.IsCalculating);
51 |
52 | // Cancel command
53 | CancelProcess = ReactiveCommand.Create(() => _ctsCancel?.Cancel(true));
54 | }
55 |
56 | private async Task ProcessImagesImpl()
57 | {
58 | _ctsCancel = new CancellationTokenSource();
59 |
60 | var sw = new Stopwatch();
61 | sw.Start();
62 |
63 | try
64 | {
65 | await Task.Run(() =>
66 | {
67 | foreach (var filename in System.IO.Directory.EnumerateFiles(Environment.CurrentDirectory, "*.png"))
68 | {
69 | for (int i = 0; i < 500; i++)
70 | {
71 | _ctsCancel.Token.ThrowIfCancellationRequested();
72 |
73 | using var src = new Mat(filename);
74 | using var resut = src.Canny(50, 200);
75 | }
76 |
77 | //using (new Window($"src image {filename}", src))
78 | //using (new Window($"resut image {filename}", resut))
79 | //{
80 | // //Cv2.WaitKey();
81 | //}
82 | }
83 | });
84 |
85 | ResultText = $"Process took {sw.Elapsed.TotalSeconds} seconds";
86 | }
87 | catch (OperationCanceledException)
88 | {
89 | ResultText = "Cancelled";
90 | }
91 | }
92 |
93 | private async Task ProcessImagesParallelImpl()
94 | {
95 | _ctsCancel = new CancellationTokenSource();
96 |
97 | var sw = new Stopwatch();
98 | sw.Start();
99 |
100 | try
101 | {
102 | await Task.Run(() =>
103 | {
104 | try
105 | {
106 | Parallel.ForEach(System.IO.Directory.EnumerateFiles(Environment.CurrentDirectory, "*.png"), (filename) =>
107 | {
108 | for (int i = 0; i < 500; i++)
109 | {
110 | _ctsCancel.Token.ThrowIfCancellationRequested();
111 |
112 | using var src = new Mat(filename);
113 | using var resut = src.Canny(50, 200);
114 | }
115 |
116 | //using (new Window($"src image {filename}", src))
117 | //using (new Window($"resut image {filename}", resut))
118 | //{
119 | // //Cv2.WaitKey();
120 | //}
121 | });
122 | }
123 | catch (AggregateException ex) when (ex.InnerExceptions.Any(x => x is OperationCanceledException))
124 | {
125 | // Flat cancellation on any ParallelForEach as a simple Cancel
126 | throw new OperationCanceledException();
127 | }
128 | });
129 |
130 | ResultText = $"Process took {sw.Elapsed.TotalSeconds} seconds";
131 | }
132 | catch (OperationCanceledException)
133 | {
134 | ResultText = "Cancelled";
135 | }
136 | }
137 |
138 | private async Task ProcessWebImpl()
139 | {
140 | _ctsCancel = new CancellationTokenSource();
141 |
142 | var sw = new Stopwatch();
143 | sw.Start();
144 |
145 | try
146 | {
147 | var websites = new List() { "https://www.dotnetfoundation.org", "https://twitter.com/GerardoLijs", "https://github.com/gerardo-lijs" };
148 |
149 | foreach (var web in websites)
150 | {
151 | // Check cancel
152 | _ctsCancel.Token.ThrowIfCancellationRequested();
153 |
154 | // Download web
155 | using (var client = new System.Net.Http.HttpClient())
156 | {
157 | string response = await client.GetStringAsync(web);
158 | }
159 | }
160 |
161 | ResultText = $"Process took {sw.Elapsed.TotalSeconds} seconds";
162 | }
163 | catch (OperationCanceledException)
164 | {
165 | ResultText = "Cancelled";
166 | }
167 | }
168 |
169 | private async Task ProcessWebParallelImpl()
170 | {
171 | _ctsCancel = new CancellationTokenSource();
172 |
173 | var sw = new Stopwatch();
174 | sw.Start();
175 |
176 | try
177 | {
178 | var websites = new List() { "https://www.dotnetfoundation.org", "https://twitter.com/GerardoLijs", "https://github.com/gerardo-lijs" };
179 |
180 | // Option 1 - Use a local function
181 | async Task DownloadWeb(string web)
182 | {
183 | // Download web
184 | using (var client = new System.Net.Http.HttpClient())
185 | {
186 | string response = await client.GetStringAsync(web);
187 | }
188 |
189 | // Check cancel
190 | _ctsCancel.Token.ThrowIfCancellationRequested();
191 | }
192 |
193 | // Option 2 - Use a lambda if you prefer
194 | //Func downloadWeb = async (web) =>
195 | //{
196 | // // Download web
197 | // using (var client = new System.Net.Http.HttpClient())
198 | // {
199 | // string response = await client.GetStringAsync(web);
200 | // }
201 |
202 | // // Check cancel
203 | // _ctsCancel.Token.ThrowIfCancellationRequested();
204 | //};
205 |
206 | // Option 3 - Use a normal Task returning method in the class
207 |
208 | var downloadTasks = websites.Select(x => DownloadWeb(x));
209 | await Task.WhenAll(downloadTasks);
210 |
211 | ResultText = $"Process took {sw.Elapsed.TotalSeconds} seconds";
212 | }
213 | catch (OperationCanceledException)
214 | {
215 | ResultText = "Cancelled";
216 | }
217 | }
218 | }
219 | }
220 |
--------------------------------------------------------------------------------