#WIP Revert Chapter 22 changes due to better testing capabilities
Need to check CS1806
MODIFY
src/Chapter22/Listing22.01.UnsychronizedState.cs
src/Chapter22/Listing22.01.UnsychronizedState.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_01
{
using System;
+ using System.Threading;
using System.Threading.Tasks;
public class Program
{
static int _Total = int.MaxValue;
- static long _Count = 0;
+ static int _Count = 0;
- public static void Main()
+ public static int Main(string[] args)
{
+ if (args?.Length > 0) { int.TryParse(args[0], out _Total); }
+
+ Console.WriteLine($"Increment and decrementing {_Total} times...");
+
// Use Task.Factory.StartNew for .NET 4.0
Task task = Task.Run(() => Decrement());
@@ -21,6 +26,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_01
task.Wait();
Console.WriteLine($"Count = {_Count}");
+
+ return _Count;
}
static void Decrement()

MODIFY
src/Chapter22/Listing22.02.UnsychronizedLocalVariables.cs
src/Chapter22/Listing22.02.UnsychronizedLocalVariables.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_02
{
using System;
+ using System.Threading;
using System.Threading.Tasks;
public class Program
{
- public static void Main()
+ static public CancellationToken CancellationToken;
+
+ public static int Main(string[] args)
{
+ int total = int.MaxValue;
int x = 0;
- Parallel.For(0, int.MaxValue, i =>
+ if (args?.Length > 0) { int.TryParse(args[0], out total); }
+ Console.WriteLine($"Increment and decrementing {total} times...");
+
+ Parallel.For(0, total,
+ new ParallelOptions() { CancellationToken = CancellationToken}
+ , i =>
{
x++;
x--;
});
Console.WriteLine("Count = {0}", x);
+ return x;
}
}
}

MODIFY
src/Chapter22/Listing22.03.SynchronizingWithMonitor.cs
src/Chapter22/Listing22.03.SynchronizingWithMonitor.cs
@@ -8,10 +8,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_03
{
readonly static object _Sync = new object();
static int _Total = int.MaxValue;
- static long _Count = 0;
+ static int _Count = 0;
- public static void Main()
+ public static int Main(string[] args)
{
+ if (args?.Length > 0) { int.TryParse(args[0], out _Total); }
+ Console.WriteLine($"Increment and decrementing {_Total} times...");
+
// Use Task.Factory.StartNew for .NET 4.0
Task task = Task.Run(() => Decrement());
@@ -35,6 +38,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_03
task.Wait();
Console.WriteLine($"Count = {_Count}");
+ return _Count;
}
static void Decrement()

MODIFY
src/Chapter22/Listing22.04.SynchronizingWithLockKeyword.cs
src/Chapter22/Listing22.04.SynchronizingWithLockKeyword.cs
@@ -7,10 +7,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_04
{
readonly static object _Sync = new object();
static int _Total = int.MaxValue;
- static long _Count = 0;
+ static int _Count = 0;
- public static void Main()
+ public static int Main(string[] args)
{
+ if (args?.Length > 0) { int.TryParse(args[0], out _Total); }
+ Console.WriteLine($"Increment and decrementing {_Total} times...");
+
// Use Task.Factory.StartNew for .NET 4.0
Task task = Task.Run(() => Decrement());
@@ -25,6 +28,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_04
task.Wait();
Console.WriteLine($"Count = {_Count}");
+ return _Count;
}
static void Decrement()

MODIFY
src/Chapter22/Listing22.05.AsyncMain.cs
src/Chapter22/Listing22.05.AsyncMain.cs
@@ -7,10 +7,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_05
{
readonly static object _Sync = new object();
static int _Total = int.MaxValue;
- static long _Count = 0;
+ static int _Count = 0;
- public static async Task Main()
+ public static async Task<int> Main(string[] args)
{
+ if (args?.Length > 0) { int.TryParse(args[0], out _Total); }
+ Console.WriteLine($"Increment and decrementing {_Total} times...");
+
// Use Task.Factory.StartNew for .NET 4.0
Task task = Task.Run(() => Decrement());
@@ -25,6 +28,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter22.Listing22_05
await task;
Console.WriteLine($"Count = {_Count}");
+ return _Count;
}
static void Decrement()

#WIP Revert Chapter 22 changes due to better testing capabilities

Need to check CS1806