From b362e80cce25835ddcc22a408a5bfe5659c62774 Mon Sep 17 00:00:00 2001 From: Lorderot Date: Fri, 12 May 2017 00:47:30 +0300 Subject: [PATCH 1/4] Added CSharp examples --- CSharp/CancellationTokenExample.cs | 64 ++++++++++++++++++++++++++++ CSharp/Extensions.cs | 39 +++++++++++++++++ CSharp/ParallelExecutionExample.cs | 39 +++++++++++++++++ CSharp/SequentialExecutionExample.cs | 64 ++++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 CSharp/CancellationTokenExample.cs create mode 100644 CSharp/Extensions.cs create mode 100644 CSharp/ParallelExecutionExample.cs create mode 100644 CSharp/SequentialExecutionExample.cs diff --git a/CSharp/CancellationTokenExample.cs b/CSharp/CancellationTokenExample.cs new file mode 100644 index 0000000..9db1289 --- /dev/null +++ b/CSharp/CancellationTokenExample.cs @@ -0,0 +1,64 @@ +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace AsyncAndPromises +{ + public class CancellationTokenExample + { + static readonly Random Random = new Random(); + static readonly CancellationTokenSource cancelationSource = new CancellationTokenSource(); + + public static void Run() + { + Console.WriteLine("Cancelation Token example."); + var stringBuilder = new StringBuilder(); + + var h = new Task(() => AddSymbol(stringBuilder, 'H'), cancelationSource.Token); + var e = new Task(() => AddSymbol(stringBuilder, 'e'), cancelationSource.Token); + var l = new Task(() => AddSymbol(stringBuilder, 'l'), cancelationSource.Token); + var o = new Task(() => AddSymbol(stringBuilder, 'o'), cancelationSource.Token); + + Console.WriteLine("Press enter to stop execution"); + + h.Start(); + var promise = h.Then(e) + .Then(l) + .Then(new Task(() => AddSymbol(stringBuilder, 'l'))) + .Then(o); + + + Console.ReadLine(); + //Throw cancel request + cancelationSource.Cancel(); + + try + { + //wait when all threads will be stopped + promise.Wait(cancelationSource.Token); + } + catch (OperationCanceledException ex) + { + //it's expected exception + } + finally + { + Console.WriteLine("Result: {0}", stringBuilder); + Console.ReadLine(); + } + } + + public static void AddSymbol(StringBuilder stringBuilder, + char symbol) + { + //1-3 second + Thread.Sleep(Random.Next(1000, 3000)); + + //Check if cancellation was requested + cancelationSource.Token.ThrowIfCancellationRequested(); + stringBuilder.Append(symbol); + Console.WriteLine("Appended '{0}' symbol", symbol); + } + } +} \ No newline at end of file diff --git a/CSharp/Extensions.cs b/CSharp/Extensions.cs new file mode 100644 index 0000000..c9092fa --- /dev/null +++ b/CSharp/Extensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AsyncAndPromises +{ + /// + /// Just add custom methods to Existing classes. + /// It makes syntax like JavaScript + /// + public static class Extensions + { + public static Task Then(this Task task, Task nextTask) + { + task.ContinueWith(t => + { + if (!nextTask.IsCanceled) + nextTask.Start(); + }); + + return nextTask; + } + + public static Task Then(this Task task, Action nextAction) + { + var nextTask = new Task(nextAction); + return task.Then(nextTask); + } + + public static Task Then(this Action action, Action nextAction) + { + var rootTask = new Task(action); + rootTask.Start(); + return rootTask.Then(nextAction); + } + } +} diff --git a/CSharp/ParallelExecutionExample.cs b/CSharp/ParallelExecutionExample.cs new file mode 100644 index 0000000..885b1a3 --- /dev/null +++ b/CSharp/ParallelExecutionExample.cs @@ -0,0 +1,39 @@ +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace AsyncAndPromises +{ + public class ParallelExecutionExample + { + static readonly Random Random = new Random(); + + public static void SequentialPromiseExample() + { + var stringBuilder = new StringBuilder(); + + var h = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'H')); + var e = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'e')); + var l = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'l')); + var o = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'o')); + + //Action will be executed parallely + //Wait for all + Task.WaitAll(h, e, l, o); + + Console.WriteLine(stringBuilder.ToString()); + Console.ReadLine(); + } + + public static void AddSymbol(StringBuilder stringBuilder, + char symbol) + { + //1-3 seconds + Thread.Sleep(Random.Next(1000, 3000)); + + stringBuilder.Append(symbol); + Console.WriteLine("Appended '{0}' symbol", symbol); + } + } +} \ No newline at end of file diff --git a/CSharp/SequentialExecutionExample.cs b/CSharp/SequentialExecutionExample.cs new file mode 100644 index 0000000..89d27b7 --- /dev/null +++ b/CSharp/SequentialExecutionExample.cs @@ -0,0 +1,64 @@ +using System; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace AsyncAndPromises +{ + public class SequentialExecutionExample + { + static readonly Random Random = new Random(); + + public static void SequentialPromiseExample() + { + var stringBuilder = new StringBuilder(); + + var h = new Task(() => AddSymbol(stringBuilder, 'H')); + var e = new Task(() => AddSymbol(stringBuilder, 'e')); + var l = new Task(() => AddSymbol(stringBuilder, 'l')); + var o = new Task(() => AddSymbol(stringBuilder, 'o')); + + //Functions will be executed in order as it was specified + h.Start(); + var promise = h.Then(e) + .Then(l) + .Then(new Task(() => AddSymbol(stringBuilder, 'l'))) + .Then(o); + + promise.Wait(); + + Console.WriteLine(stringBuilder.ToString()); + Console.ReadLine(); + } + + public static void SequentialPromiseExampleImprovedSyntax() + { + var stringBuilder = new StringBuilder(); + + Action h = () => { AddSymbol(stringBuilder, 'H'); }; + Action e = () => { AddSymbol(stringBuilder, 'e'); }; + Action l = () => { AddSymbol(stringBuilder, 'l'); }; + Action o = () => { AddSymbol(stringBuilder, 'o'); }; + + var promise = h.Then(e); + promise = promise.Then(l) + .Then(l) + .Then(o); + + promise.Wait(); + + Console.WriteLine(stringBuilder.ToString()); + Console.ReadLine(); + } + + public static void AddSymbol(StringBuilder stringBuilder, + char symbol) + { + //1-3 seconds + Thread.Sleep(Random.Next(1000, 3000)); + + stringBuilder.Append(symbol); + Console.WriteLine("Appended '{0}' symbol", symbol); + } + } +} \ No newline at end of file From 49971cf442803cc81974f6c61bb8bcccd1c1e215 Mon Sep 17 00:00:00 2001 From: Lorderot Date: Fri, 12 May 2017 00:52:19 +0300 Subject: [PATCH 2/4] Create README.md --- CSharp/README.MD | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 CSharp/README.MD diff --git a/CSharp/README.MD b/CSharp/README.MD new file mode 100644 index 0000000..86b9a41 --- /dev/null +++ b/CSharp/README.MD @@ -0,0 +1,7 @@ +AsynchronousProgramming + +Parallel execution + +Sequential execution + +Cancellation Token From 5a846f47b403c66cf6419c6bb38dabf9ee538f46 Mon Sep 17 00:00:00 2001 From: Lorderot Date: Fri, 12 May 2017 01:03:14 +0300 Subject: [PATCH 3/4] Update README.md --- CSharp/README.MD | 58 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/CSharp/README.MD b/CSharp/README.MD index 86b9a41..fd0707e 100644 --- a/CSharp/README.MD +++ b/CSharp/README.MD @@ -1,7 +1,57 @@ -AsynchronousProgramming +

C# Examples

-Parallel execution +## Parallel execution -Sequential execution +```csharp + var h = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'H')); + var e = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'e')); + var l = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'l')); + var o = Task.Factory.StartNew(() => AddSymbol(stringBuilder, 'o')); -Cancellation Token + //Action will be executed parallely + //Wait for all + Task.WaitAll(h, e, l, o); +``` +Result: +![image](https://cloud.githubusercontent.com/assets/12159879/25973800/53eb500c-36ae-11e7-9ef3-43cb3c2b3331.png) + +## Sequential execution + +```csharp +Action h = () => { AddSymbol(stringBuilder, 'H'); }; +Action e = () => { AddSymbol(stringBuilder, 'e'); }; +Action l = () => { AddSymbol(stringBuilder, 'l'); }; +Action o = () => { AddSymbol(stringBuilder, 'o'); }; + +var promise = h.Then(e); +promise = promise.Then(l) + .Then(l) + .Then(o); + +``` +Result: +![image](https://cloud.githubusercontent.com/assets/12159879/25973791/48de1438-36ae-11e7-85c1-ffc096d1eb4c.png) + +## Cancellation Token + +```csharp + var h = new Task(() => AddSymbol(stringBuilder, 'H'), cancelationSource.Token); + var e = new Task(() => AddSymbol(stringBuilder, 'e'), cancelationSource.Token); + var l = new Task(() => AddSymbol(stringBuilder, 'l'), cancelationSource.Token); + var o = new Task(() => AddSymbol(stringBuilder, 'o'), cancelationSource.Token); + + Console.WriteLine("Press enter to stop execution"); + + h.Start(); + var promise = h.Then(e) + .Then(l) + .Then(new Task(() => AddSymbol(stringBuilder, 'l'))) + .Then(o); + + + Console.ReadLine(); + //Throw cancel request + cancelationSource.Cancel(); +``` +Result: +![image](https://cloud.githubusercontent.com/assets/12159879/25973809/63b8e9cc-36ae-11e7-830b-01d985cfb4d6.png) From 5cdb53ab0532ac9ca24142e5555a9f3fb0150823 Mon Sep 17 00:00:00 2001 From: Lorderot Date: Wed, 24 May 2017 01:30:08 +0300 Subject: [PATCH 4/4] Added new lines at the end --- CSharp/CancellationTokenExample.cs | 2 +- CSharp/ParallelExecutionExample.cs | 2 +- CSharp/SequentialExecutionExample.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CSharp/CancellationTokenExample.cs b/CSharp/CancellationTokenExample.cs index 9db1289..f23482b 100644 --- a/CSharp/CancellationTokenExample.cs +++ b/CSharp/CancellationTokenExample.cs @@ -61,4 +61,4 @@ public static void AddSymbol(StringBuilder stringBuilder, Console.WriteLine("Appended '{0}' symbol", symbol); } } -} \ No newline at end of file +} diff --git a/CSharp/ParallelExecutionExample.cs b/CSharp/ParallelExecutionExample.cs index 885b1a3..01eab13 100644 --- a/CSharp/ParallelExecutionExample.cs +++ b/CSharp/ParallelExecutionExample.cs @@ -36,4 +36,4 @@ public static void AddSymbol(StringBuilder stringBuilder, Console.WriteLine("Appended '{0}' symbol", symbol); } } -} \ No newline at end of file +} diff --git a/CSharp/SequentialExecutionExample.cs b/CSharp/SequentialExecutionExample.cs index 89d27b7..01766d0 100644 --- a/CSharp/SequentialExecutionExample.cs +++ b/CSharp/SequentialExecutionExample.cs @@ -61,4 +61,4 @@ public static void AddSymbol(StringBuilder stringBuilder, Console.WriteLine("Appended '{0}' symbol", symbol); } } -} \ No newline at end of file +}