├── .gitattributes ├── .gitignore ├── .nuget ├── NuGet.Config ├── NuGet.exe └── NuGet.targets ├── BookSamples_VS2013ExpressWindows.sln ├── BookSamples_VS2013ExpressWindowsDesktop.sln ├── BookSamples_VisualStudio2013.sln ├── Chapter1 ├── Recipe1 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe10 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe10.csproj ├── Recipe11 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe11.csproj ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Recipe6 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe6.csproj ├── Recipe7 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe7.csproj ├── Recipe8 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe8.csproj └── Recipe9 │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe9.csproj ├── Chapter10 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe3.csproj │ └── packages.config └── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Chapter11 ├── Recipe1 │ ├── App.xaml │ ├── App.xaml.cs │ ├── Assets │ │ ├── Logo.png │ │ ├── SmallLogo.png │ │ ├── SplashScreen.png │ │ └── StoreLogo.png │ ├── Common │ │ └── StandardStyles.xaml │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Package.appxmanifest │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj └── Recipe3 │ ├── App.xaml │ ├── App.xaml.cs │ ├── Assets │ ├── Logo.png │ ├── SmallLogo-Badge.png │ ├── SmallLogo.png │ ├── SplashScreen.png │ └── StoreLogo.png │ ├── Common │ └── StandardStyles.xaml │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Package.appxmanifest │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Chapter2 ├── Recipe1 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Recipe6 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe6.csproj ├── Recipe7 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe7.csproj ├── Recipe8 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe8.csproj └── Recipe9 │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe9.csproj ├── Chapter3 ├── Recipe1 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Recipe6 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe6.csproj └── Recipe7 │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe7.csproj ├── Chapter4 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Recipe6 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe6.csproj ├── Recipe7 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe7.csproj ├── Recipe8 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe8.csproj └── Recipe9 │ ├── App.config │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings │ └── Recipe9.csproj ├── Chapter5 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Recipe6 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe6.csproj ├── Recipe7 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe7.csproj ├── Recipe8 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe8.csproj └── Recipe9 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Recipe9.csproj │ └── packages.config ├── Chapter6 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj └── Recipe5 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe5.csproj ├── Chapter7 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj ├── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── Recipe5 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe5.csproj └── Recipe6 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe6.csproj ├── Chapter8 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe1.csproj │ └── packages.config ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe2.csproj │ └── packages.config ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe3.csproj │ └── packages.config ├── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe4.csproj │ └── packages.config ├── Recipe5 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe5.csproj │ └── packages.config └── Recipe6 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Recipe6.csproj │ └── packages.config ├── Chapter9 ├── Recipe1 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.config │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe3.csproj └── Recipe4 │ ├── App.config │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── Recipe4.csproj ├── LICENSE ├── README.md └── test └── test.sln /.nuget/NuGet.Config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.nuget/NuGet.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/.nuget/NuGet.exe -------------------------------------------------------------------------------- /Chapter1/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe1 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Thread t = new Thread(PrintNumbers); 11 | t.Start(); 12 | PrintNumbers(); 13 | } 14 | 15 | static void PrintNumbers() 16 | { 17 | Console.WriteLine("Starting..."); 18 | for (int i = 1; i < 10; i++) 19 | { 20 | Console.WriteLine(i); 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter1/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0ceeb3db-93f2-4154-9013-d777321f3f17")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe10/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe10 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | object lock1 = new object(); 11 | object lock2 = new object(); 12 | 13 | new Thread(() => LockTooMuch(lock1, lock2)).Start(); 14 | 15 | lock (lock2) 16 | { 17 | Thread.Sleep(1000); 18 | Console.WriteLine("Monitor.TryEnter allows not to get stuck, returning false after a specified timeout is elapsed"); 19 | if (Monitor.TryEnter(lock1, TimeSpan.FromSeconds(5))) 20 | { 21 | Console.WriteLine("Acquired a protected resource succesfully"); 22 | } 23 | else 24 | { 25 | Console.WriteLine("Timeout acquiring a resource!"); 26 | } 27 | } 28 | 29 | new Thread(() => LockTooMuch(lock1, lock2)).Start(); 30 | 31 | Console.WriteLine("----------------------------------"); 32 | lock (lock2) 33 | { 34 | Console.WriteLine("This will be a deadlock!"); 35 | Thread.Sleep(1000); 36 | lock (lock1) 37 | { 38 | Console.WriteLine("Acquired a protected resource succesfully"); 39 | } 40 | } 41 | } 42 | 43 | static void LockTooMuch(object lock1, object lock2) 44 | { 45 | lock (lock1) 46 | { 47 | Thread.Sleep(1000); 48 | lock (lock2); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Chapter1/Recipe10/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe10")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe10")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("27746d59-c51e-4c29-a543-f9b3255fbc9d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe11/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe11 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var t = new Thread(FaultyThread); 11 | t.Start(); 12 | t.Join(); 13 | 14 | try 15 | { 16 | t = new Thread(BadFaultyThread); 17 | t.Start(); 18 | } 19 | catch (Exception ex) 20 | { 21 | Console.WriteLine("We won't get here!"); 22 | } 23 | } 24 | 25 | static void BadFaultyThread() 26 | { 27 | Console.WriteLine("Starting a faulty thread..."); 28 | Thread.Sleep(TimeSpan.FromSeconds(2)); 29 | throw new Exception("Boom!"); 30 | } 31 | 32 | static void FaultyThread() 33 | { 34 | try 35 | { 36 | Console.WriteLine("Starting a faulty thread..."); 37 | Thread.Sleep(TimeSpan.FromSeconds(1)); 38 | throw new Exception("Boom!"); 39 | } 40 | catch (Exception ex) 41 | { 42 | Console.WriteLine("Exception handled: {0}", ex.Message); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Chapter1/Recipe11/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe14")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe14")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fbf98b90-1596-4fca-bafb-44ce73b4718a")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe2/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe2 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Thread t = new Thread(PrintNumbersWithDelay); 11 | t.Start(); 12 | PrintNumbers(); 13 | } 14 | 15 | static void PrintNumbers() 16 | { 17 | Console.WriteLine("Starting..."); 18 | for (int i = 1; i < 10; i++) 19 | { 20 | Console.WriteLine(i); 21 | } 22 | } 23 | 24 | static void PrintNumbersWithDelay() 25 | { 26 | Console.WriteLine("Starting..."); 27 | for (int i = 1; i < 10; i++) 28 | { 29 | Thread.Sleep(TimeSpan.FromSeconds(2)); 30 | Console.WriteLine(i); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Chapter1/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("28b0ed05-0050-4d9f-b814-26dd1d236523")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe3/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe3 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Console.WriteLine("Starting program..."); 11 | Thread t = new Thread(PrintNumbersWithDelay); 12 | t.Start(); 13 | t.Join(); 14 | Console.WriteLine("Thread completed"); 15 | } 16 | 17 | static void PrintNumbersWithDelay() 18 | { 19 | Console.WriteLine("Starting..."); 20 | for (int i = 1; i < 10; i++) 21 | { 22 | Thread.Sleep(TimeSpan.FromSeconds(2)); 23 | Console.WriteLine(i); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter1/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("56583f3d-119b-4aca-840b-bf2ee53db6fd")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe4/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe4 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Console.WriteLine("Starting program..."); 11 | Thread t = new Thread(PrintNumbersWithDelay); 12 | t.Start(); 13 | Thread.Sleep(TimeSpan.FromSeconds(6)); 14 | t.Abort(); 15 | Console.WriteLine("A thread has been aborted"); 16 | } 17 | 18 | static void PrintNumbersWithDelay() 19 | { 20 | Console.WriteLine("Starting..."); 21 | for (int i = 1; i < 10; i++) 22 | { 23 | Thread.Sleep(TimeSpan.FromSeconds(2)); 24 | Console.WriteLine(i); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Chapter1/Recipe4/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe4")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe4")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("78b7a1e7-835b-4aec-a556-94d6b76e7080")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe5/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe5 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Console.WriteLine("Starting program..."); 11 | Thread t = new Thread(PrintNumbersWithStatus); 12 | Thread t2 = new Thread(DoNothing); 13 | Console.WriteLine(t.ThreadState.ToString()); 14 | t2.Start(); 15 | t.Start(); 16 | for (int i = 1; i < 30; i++) 17 | { 18 | Console.WriteLine(t.ThreadState.ToString()); 19 | } 20 | Thread.Sleep(TimeSpan.FromSeconds(6)); 21 | t.Abort(); 22 | Console.WriteLine("A thread has been aborted"); 23 | Console.WriteLine(t.ThreadState.ToString()); 24 | Console.WriteLine(t2.ThreadState.ToString()); 25 | } 26 | 27 | static void DoNothing() 28 | { 29 | Thread.Sleep(TimeSpan.FromSeconds(2)); 30 | } 31 | 32 | static void PrintNumbersWithStatus() 33 | { 34 | Console.WriteLine("Starting..."); 35 | Console.WriteLine(Thread.CurrentThread.ThreadState.ToString()); 36 | for (int i = 1; i < 10; i++) 37 | { 38 | Thread.Sleep(TimeSpan.FromSeconds(2)); 39 | Console.WriteLine(i); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Chapter1/Recipe5/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe5")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe5")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3004638c-a1c6-4c9a-ae23-8b1c080d5682")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe6/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | 5 | namespace Chapter1.Recipe6 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Console.WriteLine("Current thread priority: {0}", Thread.CurrentThread.Priority); 12 | Console.WriteLine("Running on all cores available"); 13 | RunThreads(); 14 | Thread.Sleep(TimeSpan.FromSeconds(2)); 15 | Console.WriteLine("Running on a single core"); 16 | Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1); 17 | RunThreads(); 18 | } 19 | 20 | static void RunThreads() 21 | { 22 | var sample = new ThreadSample(); 23 | 24 | var threadOne = new Thread(sample.CountNumbers); 25 | threadOne.Name = "ThreadOne"; 26 | var threadTwo = new Thread(sample.CountNumbers); 27 | threadTwo.Name = "ThreadTwo"; 28 | 29 | threadOne.Priority = ThreadPriority.Highest; 30 | threadTwo.Priority = ThreadPriority.Lowest; 31 | threadOne.Start(); 32 | threadTwo.Start(); 33 | 34 | Thread.Sleep(TimeSpan.FromSeconds(2)); 35 | sample.Stop(); 36 | } 37 | 38 | class ThreadSample 39 | { 40 | private bool _isStopped = false; 41 | 42 | public void Stop() 43 | { 44 | _isStopped = true; 45 | } 46 | 47 | public void CountNumbers() 48 | { 49 | long counter = 0; 50 | 51 | while (!_isStopped) 52 | { 53 | counter++; 54 | } 55 | 56 | Console.WriteLine("{0} with {1,11} priority " + 57 | "has a count = {2,13}", Thread.CurrentThread.Name, 58 | Thread.CurrentThread.Priority, 59 | counter.ToString("N0")); 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Chapter1/Recipe6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe6")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe6")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2b30c7ae-5ce3-49bb-9cdf-d2516d2290be")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe7/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe7 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var sampleForeground = new ThreadSample(10); 11 | var sampleBackground = new ThreadSample(20); 12 | 13 | var threadOne = new Thread(sampleForeground.CountNumbers); 14 | threadOne.Name = "ForegroundThread"; 15 | var threadTwo = new Thread(sampleBackground.CountNumbers); 16 | threadTwo.Name = "BackgroundThread"; 17 | threadTwo.IsBackground = true; 18 | 19 | threadOne.Start(); 20 | threadTwo.Start(); 21 | } 22 | 23 | class ThreadSample 24 | { 25 | private readonly int _iterations; 26 | 27 | public ThreadSample(int iterations) 28 | { 29 | _iterations = iterations; 30 | } 31 | public void CountNumbers() 32 | { 33 | for (int i = 0; i < _iterations; i++) 34 | { 35 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 36 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i); 37 | } 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter1/Recipe7/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe7")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe7")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("49a9121c-996a-4eb5-a0e8-e86475e0dd16")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe8/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter1.Recipe8 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var sample = new ThreadSample(10); 11 | 12 | var threadOne = new Thread(sample.CountNumbers); 13 | threadOne.Name = "ThreadOne"; 14 | threadOne.Start(); 15 | threadOne.Join(); 16 | 17 | Console.WriteLine("--------------------------"); 18 | 19 | var threadTwo = new Thread(Count); 20 | threadTwo.Name = "ThreadTwo"; 21 | threadTwo.Start(8); 22 | threadTwo.Join(); 23 | 24 | Console.WriteLine("--------------------------"); 25 | 26 | var threadThree = new Thread(() => CountNumbers(12)); 27 | threadThree.Name = "ThreadThree"; 28 | threadThree.Start(); 29 | threadThree.Join(); 30 | Console.WriteLine("--------------------------"); 31 | 32 | int i = 10; 33 | var threadFour = new Thread(() => PrintNumber(i)); 34 | i = 20; 35 | var threadFive = new Thread(() => PrintNumber(i)); 36 | threadFour.Start(); 37 | threadFive.Start(); 38 | } 39 | 40 | static void Count(object iterations) 41 | { 42 | CountNumbers((int)iterations); 43 | } 44 | 45 | static void CountNumbers(int iterations) 46 | { 47 | for (int i = 1; i <= iterations; i++) 48 | { 49 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 50 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i); 51 | } 52 | } 53 | 54 | static void PrintNumber(int number) 55 | { 56 | Console.WriteLine(number); 57 | } 58 | 59 | class ThreadSample 60 | { 61 | private readonly int _iterations; 62 | 63 | public ThreadSample(int iterations) 64 | { 65 | _iterations = iterations; 66 | } 67 | public void CountNumbers() 68 | { 69 | for (int i = 1; i <= _iterations; i++) 70 | { 71 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 72 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i); 73 | } 74 | } 75 | } 76 | } 77 | 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Chapter1/Recipe8/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe8")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe8")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2cbac03d-c6da-4d55-98cd-9bdfa2e11564")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter1/Recipe9/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe9")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe9")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("a07629bb-ca3f-42d6-8ea6-c99bf31637a8")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter10/Recipe1/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter10/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("52b0193d-762f-4d4f-a8fe-7c86237d1270")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter10/Recipe2/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter10/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("be99ea07-302f-490c-a775-f1706d4d10c0")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter10/Recipe3/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter10/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f399b1fb-7f3f-4bbc-807a-9ab99f4f1d1f")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter10/Recipe3/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter10/Recipe4/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter10/Recipe4/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe4")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe4")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("63702efc-ff1e-43cb-97d4-6ae930b2feb7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/App.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/Logo.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/SmallLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/SmallLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/SplashScreen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/SplashScreen.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/StoreLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Chapter11.Recipe1 6 | Eugene 7 | Assets\StoreLogo.png 8 | 9 | 10 | 6.3.0 11 | 6.3.0 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Version information for an assembly consists of the following four values: 18 | // 19 | // Major Version 20 | // Minor Version 21 | // Build Number 22 | // Revision 23 | // 24 | // You can specify all the values or you can default the Build and Revision Numbers 25 | // by using the '*' as shown below: 26 | // [assembly: AssemblyVersion("1.0.*")] 27 | [assembly: AssemblyVersion("1.0.0.0")] 28 | [assembly: AssemblyFileVersion("1.0.0.0")] 29 | [assembly: ComVisible(false)] -------------------------------------------------------------------------------- /Chapter11/Recipe2/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter11/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e6053ad8-4fb1-42ff-8b68-55932e053dfc")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/App.xaml: -------------------------------------------------------------------------------- 1 |  6 | 7 | 8 | 9 | 10 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/Logo.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/SmallLogo-Badge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SmallLogo-Badge.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/SmallLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SmallLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/SplashScreen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SplashScreen.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/StoreLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Recipe3 6 | Eugene 7 | Assets\StoreLogo.png 8 | 9 | 10 | 6.3.0 11 | 6.3.0 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Version information for an assembly consists of the following four values: 18 | // 19 | // Major Version 20 | // Minor Version 21 | // Build Number 22 | // Revision 23 | // 24 | // You can specify all the values or you can default the Build and Revision Numbers 25 | // by using the '*' as shown below: 26 | // [assembly: AssemblyVersion("1.0.*")] 27 | [assembly: AssemblyVersion("1.0.0.0")] 28 | [assembly: AssemblyFileVersion("1.0.0.0")] 29 | [assembly: ComVisible(false)] -------------------------------------------------------------------------------- /Chapter2/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe1 5 | { 6 | internal class Program 7 | { 8 | private static void Main(string[] args) 9 | { 10 | Console.WriteLine("Incorrect counter"); 11 | 12 | var c = new Counter(); 13 | 14 | var t1 = new Thread(() => TestCounter(c)); 15 | var t2 = new Thread(() => TestCounter(c)); 16 | var t3 = new Thread(() => TestCounter(c)); 17 | t1.Start(); 18 | t2.Start(); 19 | t3.Start(); 20 | t1.Join(); 21 | t2.Join(); 22 | t3.Join(); 23 | 24 | Console.WriteLine("Total count: {0}", c.Count); 25 | Console.WriteLine("--------------------------"); 26 | 27 | Console.WriteLine("Correct counter"); 28 | 29 | var c1 = new CounterNoLock(); 30 | 31 | t1 = new Thread(() => TestCounter(c1)); 32 | t2 = new Thread(() => TestCounter(c1)); 33 | t3 = new Thread(() => TestCounter(c1)); 34 | t1.Start(); 35 | t2.Start(); 36 | t3.Start(); 37 | t1.Join(); 38 | t2.Join(); 39 | t3.Join(); 40 | 41 | Console.WriteLine("Total count: {0}", c1.Count); 42 | } 43 | 44 | static void TestCounter(CounterBase c) 45 | { 46 | for (int i = 0; i < 100000; i++) 47 | { 48 | c.Increment(); 49 | c.Decrement(); 50 | } 51 | } 52 | 53 | class Counter : CounterBase 54 | { 55 | private int _count; 56 | 57 | public int Count { get { return _count; } } 58 | 59 | public override void Increment() 60 | { 61 | _count++; 62 | } 63 | 64 | public override void Decrement() 65 | { 66 | _count--; 67 | } 68 | } 69 | 70 | class CounterNoLock : CounterBase 71 | { 72 | private int _count; 73 | 74 | public int Count { get { return _count; } } 75 | 76 | public override void Increment() 77 | { 78 | Interlocked.Increment(ref _count); 79 | } 80 | 81 | public override void Decrement() 82 | { 83 | Interlocked.Decrement(ref _count); 84 | } 85 | } 86 | 87 | abstract class CounterBase 88 | { 89 | public abstract void Increment(); 90 | 91 | public abstract void Decrement(); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Chapter2/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("944a1eb5-90f6-4365-98a0-b45118f56175")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe2/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe2 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | const string MutexName = "CSharpThreadingCookbook"; 11 | 12 | using (var m = new Mutex(false, MutexName)) 13 | { 14 | if (!m.WaitOne(TimeSpan.FromSeconds(5), false)) 15 | { 16 | Console.WriteLine("Second instance is running!"); 17 | } 18 | else 19 | { 20 | Console.WriteLine("Running!"); 21 | Console.ReadLine(); 22 | m.ReleaseMutex(); 23 | } 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter2/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c63b4870-e7e6-42b1-aba2-f0f6b24fa94f")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe3/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe3 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | for (int i = 1; i <= 6; i++) 11 | { 12 | string threadName = "Thread " + i; 13 | int secondsToWait = 2 + 2 * i; 14 | var t = new Thread(() => AccessDatabase(threadName, secondsToWait)); 15 | t.Start(); 16 | } 17 | } 18 | 19 | static SemaphoreSlim _semaphore = new SemaphoreSlim(4); 20 | 21 | static void AccessDatabase(string name, int seconds) 22 | { 23 | Console.WriteLine("{0} waits to access a database", name); 24 | _semaphore.Wait(); 25 | Console.WriteLine("{0} was granted an access to a database", name); 26 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 27 | Console.WriteLine("{0} is completed", name); 28 | _semaphore.Release(); 29 | 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Chapter2/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7fcc52db-8509-4f1a-9d02-7921a430505d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe4/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe4 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var t = new Thread(() => Process(10)); 11 | t.Start(); 12 | 13 | Console.WriteLine("Waiting for another thread to complete work"); 14 | _workerEvent.WaitOne(); 15 | Console.WriteLine("First operation is completed!"); 16 | Console.WriteLine("Performing an operation on a main thread"); 17 | Thread.Sleep(TimeSpan.FromSeconds(5)); 18 | _mainEvent.Set(); 19 | Console.WriteLine("Now running the second operation on a second thread"); 20 | _workerEvent.WaitOne(); 21 | Console.WriteLine("Second operation is completed!"); 22 | } 23 | 24 | private static AutoResetEvent _workerEvent = new AutoResetEvent(false); 25 | private static AutoResetEvent _mainEvent = new AutoResetEvent(false); 26 | 27 | static void Process(int seconds) 28 | { 29 | Console.WriteLine("Starting a long running work..."); 30 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 31 | Console.WriteLine("Work is done!"); 32 | _workerEvent.Set(); 33 | Console.WriteLine("Waiting for a main thread to complete its work"); 34 | _mainEvent.WaitOne(); 35 | Console.WriteLine("Starting second operation..."); 36 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 37 | Console.WriteLine("Work is done!"); 38 | _workerEvent.Set(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter2/Recipe4/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe4")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe4")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("8bad7962-39eb-4047-a3b7-024fda286eec")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe5/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe5 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var t1 = new Thread(() => TravelThroughGates("Thread 1", 5)); 11 | var t2 = new Thread(() => TravelThroughGates("Thread 2", 6)); 12 | var t3 = new Thread(() => TravelThroughGates("Thread 3", 12)); 13 | t1.Start(); 14 | t2.Start(); 15 | t3.Start(); 16 | Thread.Sleep(TimeSpan.FromSeconds(6)); 17 | Console.WriteLine("The gates are now open!"); 18 | _mainEvent.Set(); 19 | Thread.Sleep(TimeSpan.FromSeconds(2)); 20 | _mainEvent.Reset(); 21 | Console.WriteLine("The gates have been closed!"); 22 | Thread.Sleep(TimeSpan.FromSeconds(10)); 23 | Console.WriteLine("The gates are now open for the second time!"); 24 | _mainEvent.Set(); 25 | Thread.Sleep(TimeSpan.FromSeconds(2)); 26 | Console.WriteLine("The gates have been closed!"); 27 | _mainEvent.Reset(); 28 | } 29 | 30 | static void TravelThroughGates(string threadName, int seconds) 31 | { 32 | Console.WriteLine("{0} falls to sleep", threadName); 33 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 34 | Console.WriteLine("{0} waits for the gates to open!", threadName); 35 | _mainEvent.Wait(); 36 | Console.WriteLine("{0} enters the gates!", threadName); 37 | } 38 | 39 | static ManualResetEventSlim _mainEvent = new ManualResetEventSlim(false); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Chapter2/Recipe5/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe5")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe5")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("793034d9-1c36-4858-a4cc-7182286e0ff8")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe6/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe6 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Console.WriteLine("Starting two operations"); 11 | var t1 = new Thread(() => PerformOperation("Operation 1 is completed", 4)); 12 | var t2 = new Thread(() => PerformOperation("Operation 2 is completed", 8)); 13 | t1.Start(); 14 | t2.Start(); 15 | _countdown.Wait(); 16 | Console.WriteLine("Both operations have been completed."); 17 | _countdown.Dispose(); 18 | } 19 | 20 | static CountdownEvent _countdown = new CountdownEvent(2); 21 | 22 | static void PerformOperation(string message, int seconds) 23 | { 24 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 25 | Console.WriteLine(message); 26 | _countdown.Signal(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter2/Recipe6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe6")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe6")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7732c65d-fdd1-4322-ad33-2ab587390a3e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe7/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe7 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var t1 = new Thread(() => PlayMusic("the guitarist", "play an amazing solo", 5)); 11 | var t2 = new Thread(() => PlayMusic("the singer", "sing his song", 2)); 12 | 13 | t1.Start(); 14 | t2.Start(); 15 | } 16 | 17 | static Barrier _barrier = new Barrier(2, 18 | b => Console.WriteLine("End of phase {0}", b.CurrentPhaseNumber + 1)); 19 | 20 | static void PlayMusic(string name, string message, int seconds) 21 | { 22 | for (int i = 1; i < 3; i++) 23 | { 24 | Console.WriteLine("----------------------------------------------"); 25 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 26 | Console.WriteLine("{0} starts to {1}", name, message); 27 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 28 | Console.WriteLine("{0} finishes to {1}", name, message); 29 | _barrier.SignalAndWait(); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Chapter2/Recipe7/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe7")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe7")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c8637971-a29b-4354-8a16-c812cd0af604")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe8/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | 5 | namespace Chapter2.Recipe8 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | new Thread(Read){ IsBackground = true }.Start(); 12 | new Thread(Read){ IsBackground = true }.Start(); 13 | new Thread(Read){ IsBackground = true }.Start(); 14 | 15 | new Thread(() => Write("Thread 1")){ IsBackground = true }.Start(); 16 | new Thread(() => Write("Thread 2")){ IsBackground = true }.Start(); 17 | 18 | Thread.Sleep(TimeSpan.FromSeconds(30)); 19 | } 20 | 21 | static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim(); 22 | static Dictionary _items = new Dictionary(); 23 | 24 | static void Read() 25 | { 26 | Console.WriteLine("Reading contents of a dictionary"); 27 | while (true) 28 | { 29 | try 30 | { 31 | _rw.EnterReadLock(); 32 | foreach (var key in _items.Keys) 33 | { 34 | Thread.Sleep(TimeSpan.FromSeconds(0.1)); 35 | } 36 | } 37 | finally 38 | { 39 | _rw.ExitReadLock(); 40 | } 41 | } 42 | } 43 | 44 | static void Write(string threadName) 45 | { 46 | while (true) 47 | { 48 | try 49 | { 50 | int newKey = new Random().Next(250); 51 | _rw.EnterUpgradeableReadLock(); 52 | if (!_items.ContainsKey(newKey)) 53 | { 54 | try 55 | { 56 | _rw.EnterWriteLock(); 57 | _items[newKey] = 1; 58 | Console.WriteLine("New key {0} is added to a dictionary by a {1}", newKey, threadName); 59 | } 60 | finally 61 | { 62 | _rw.ExitWriteLock(); 63 | } 64 | } 65 | Thread.Sleep(TimeSpan.FromSeconds(0.1)); 66 | } 67 | finally 68 | { 69 | _rw.ExitUpgradeableReadLock(); 70 | } 71 | } 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Chapter2/Recipe8/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe8")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe8")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4489f834-2752-456f-8baf-103d84cf6841")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter2/Recipe9/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter2.Recipe9 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var t1 = new Thread(UserModeWait); 11 | var t2 = new Thread(HybridSpinWait); 12 | 13 | Console.WriteLine("Running user mode waiting"); 14 | t1.Start(); 15 | Thread.Sleep(20); 16 | _isCompleted = true; 17 | Thread.Sleep(TimeSpan.FromSeconds(1)); 18 | _isCompleted = false; 19 | Console.WriteLine("Running hybrid SpinWait construct waiting"); 20 | t2.Start(); 21 | Thread.Sleep(5); 22 | _isCompleted = true; 23 | } 24 | 25 | static volatile bool _isCompleted = false; 26 | 27 | static void UserModeWait() 28 | { 29 | while (!_isCompleted) 30 | { 31 | Console.Write("."); 32 | } 33 | Console.WriteLine(); 34 | Console.WriteLine("Waiting is complete"); 35 | } 36 | 37 | static void HybridSpinWait() 38 | { 39 | var w = new SpinWait(); 40 | while (!_isCompleted) 41 | { 42 | w.SpinOnce(); 43 | Console.WriteLine(w.NextSpinWillYield); 44 | } 45 | Console.WriteLine("Waiting is complete"); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Chapter2/Recipe9/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe9")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe9")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7bdbe352-3307-41db-b076-3a7d4727a1d5")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter3.Recipe1 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | int threadId = 0; 11 | 12 | RunOnThreadPool poolDelegate = Test; 13 | 14 | var t = new Thread(() => Test(out threadId)); 15 | t.Start(); 16 | t.Join(); 17 | 18 | Console.WriteLine("Thread id: {0}", threadId); 19 | 20 | IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call"); 21 | r.AsyncWaitHandle.WaitOne(); 22 | 23 | string result = poolDelegate.EndInvoke(out threadId, r); 24 | 25 | Console.WriteLine("Thread pool worker thread id: {0}", threadId); 26 | Console.WriteLine(result); 27 | 28 | Thread.Sleep(TimeSpan.FromSeconds(2)); 29 | } 30 | 31 | private delegate string RunOnThreadPool(out int threadId); 32 | 33 | private static void Callback(IAsyncResult ar) 34 | { 35 | Console.WriteLine("Starting a callback..."); 36 | Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState); 37 | Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); 38 | Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); 39 | } 40 | 41 | 42 | private static string Test(out int threadId) 43 | { 44 | Console.WriteLine("Starting..."); 45 | Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread); 46 | Thread.Sleep(TimeSpan.FromSeconds(2)); 47 | threadId = Thread.CurrentThread.ManagedThreadId; 48 | return string.Format("Thread pool worker thread id was: {0}", threadId); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Chapter3/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f6db0a1e-2c72-496a-83fa-f3d9e60a4038")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe2/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter3.Recipe2 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | const int x = 1; 11 | const int y = 2; 12 | const string lambdaState = "lambda state 2"; 13 | 14 | ThreadPool.QueueUserWorkItem(AsyncOperation); 15 | Thread.Sleep(TimeSpan.FromSeconds(1)); 16 | 17 | ThreadPool.QueueUserWorkItem(AsyncOperation, "async state"); 18 | Thread.Sleep(TimeSpan.FromSeconds(1)); 19 | 20 | ThreadPool.QueueUserWorkItem( state => { 21 | Console.WriteLine("Operation state: {0}", state); 22 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); 23 | Thread.Sleep(TimeSpan.FromSeconds(2)); 24 | }, "lambda state"); 25 | 26 | ThreadPool.QueueUserWorkItem( _ => 27 | { 28 | Console.WriteLine("Operation state: {0}, {1}", x+y, lambdaState); 29 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); 30 | Thread.Sleep(TimeSpan.FromSeconds(2)); 31 | }, "lambda state"); 32 | 33 | Thread.Sleep(TimeSpan.FromSeconds(2)); 34 | } 35 | 36 | private static void AsyncOperation(object state) 37 | { 38 | Console.WriteLine("Operation state: {0}", state ?? "(null)"); 39 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId); 40 | Thread.Sleep(TimeSpan.FromSeconds(2)); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Chapter3/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("98411a29-94b5-4fe5-84aa-4be05008e3ab")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe3/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | 5 | namespace Chapter3.Recipe3 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | const int numberOfOperations = 500; 12 | var sw = new Stopwatch(); 13 | sw.Start(); 14 | UseThreads(numberOfOperations); 15 | sw.Stop(); 16 | Console.WriteLine("Execution time using threads: {0}", sw.ElapsedMilliseconds); 17 | 18 | sw.Reset(); 19 | sw.Start(); 20 | UseThreadPool(numberOfOperations); 21 | sw.Stop(); 22 | Console.WriteLine("Execution time using threads: {0}", sw.ElapsedMilliseconds); 23 | } 24 | 25 | static void UseThreads(int numberOfOperations) 26 | { 27 | using (var countdown = new CountdownEvent(numberOfOperations)) 28 | { 29 | Console.WriteLine("Scheduling work by creating threads"); 30 | for (int i = 0; i < numberOfOperations; i++) 31 | { 32 | var thread = new Thread(() => { 33 | Console.Write("{0},", Thread.CurrentThread.ManagedThreadId); 34 | Thread.Sleep(TimeSpan.FromSeconds(0.1)); 35 | countdown.Signal(); 36 | }); 37 | thread.Start(); 38 | } 39 | countdown.Wait(); 40 | Console.WriteLine(); 41 | } 42 | } 43 | 44 | static void UseThreadPool(int numberOfOperations) 45 | { 46 | using (var countdown = new CountdownEvent(numberOfOperations)) 47 | { 48 | Console.WriteLine("Starting work on a threadpool"); 49 | for (int i = 0; i < numberOfOperations; i++) 50 | { 51 | ThreadPool.QueueUserWorkItem( _ => { 52 | Console.Write("{0},", Thread.CurrentThread.ManagedThreadId); 53 | Thread.Sleep(TimeSpan.FromSeconds(0.1)); 54 | countdown.Signal(); 55 | }); 56 | } 57 | countdown.Wait(); 58 | Console.WriteLine(); 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Chapter3/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("59d0805c-d2ea-451a-9e73-5ca0f55d3189")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe4/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe4")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe4")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("70cc3a8a-9ecb-4c51-ad54-516cbda89010")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe5/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter3.Recipe5 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | RunOperations(TimeSpan.FromSeconds(5)); 11 | RunOperations(TimeSpan.FromSeconds(7)); 12 | } 13 | 14 | static void RunOperations(TimeSpan workerOperationTimeout) 15 | { 16 | using (var evt = new ManualResetEvent(false)) 17 | using (var cts = new CancellationTokenSource()) 18 | { 19 | Console.WriteLine("Registering timeout operations..."); 20 | var worker = ThreadPool.RegisterWaitForSingleObject(evt, 21 | (state, isTimedOut) => WorkerOperationWait(cts, isTimedOut), null, workerOperationTimeout, true); 22 | 23 | Console.WriteLine("Starting long running operation..."); 24 | 25 | ThreadPool.QueueUserWorkItem(_ => WorkerOperation(cts.Token, evt)); 26 | 27 | Thread.Sleep(workerOperationTimeout.Add(TimeSpan.FromSeconds(2))); 28 | worker.Unregister(evt); 29 | } 30 | } 31 | 32 | static void WorkerOperation(CancellationToken token, ManualResetEvent evt) 33 | { 34 | for(int i = 0; i < 6; i++) 35 | { 36 | if (token.IsCancellationRequested) 37 | { 38 | return; 39 | } 40 | Thread.Sleep(TimeSpan.FromSeconds(1)); 41 | } 42 | evt.Set(); 43 | } 44 | 45 | static void WorkerOperationWait(CancellationTokenSource cts, bool isTimedOut) 46 | { 47 | if (isTimedOut) 48 | { 49 | cts.Cancel(); 50 | Console.WriteLine("Worker operation timed out and was canceled."); 51 | } 52 | else 53 | { 54 | Console.WriteLine("Worker operation succeded."); 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Chapter3/Recipe5/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe5")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe5")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("c2191460-ac35-448f-9f04-0c8ca5753006")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe6/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Chapter3.Recipe6 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | Console.WriteLine("Press 'Enter' to stop the timer..."); 11 | DateTime start = DateTime.Now; 12 | _timer = new Timer(_ => TimerOperation(start), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); 13 | 14 | Thread.Sleep(TimeSpan.FromSeconds(6)); 15 | 16 | _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(4)); 17 | 18 | Console.ReadLine(); 19 | 20 | _timer.Dispose(); 21 | } 22 | 23 | static Timer _timer; 24 | 25 | static void TimerOperation(DateTime start) 26 | { 27 | TimeSpan elapsed = DateTime.Now - start; 28 | Console.WriteLine("{0} seconds from {1}. Timer thread pool thread id: {2}", elapsed.Seconds, start, 29 | Thread.CurrentThread.ManagedThreadId); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Chapter3/Recipe6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe6")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe6")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7fb66af4-43a4-4632-8535-0fa47a07d3d1")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter3/Recipe7/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Threading; 4 | 5 | namespace Chapter3.Recipe7 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var bw = new BackgroundWorker(); 12 | bw.WorkerReportsProgress = true; 13 | bw.WorkerSupportsCancellation = true; 14 | 15 | bw.DoWork += Worker_DoWork; 16 | bw.ProgressChanged += Worker_ProgressChanged; 17 | bw.RunWorkerCompleted += Worker_Completed; 18 | 19 | bw.RunWorkerAsync(); 20 | 21 | Console.WriteLine("Press C to cancel work"); 22 | do 23 | { 24 | if (Console.ReadKey(true).KeyChar == 'C') 25 | { 26 | bw.CancelAsync(); 27 | } 28 | 29 | } 30 | while(bw.IsBusy); 31 | } 32 | 33 | static void Worker_DoWork(object sender, DoWorkEventArgs e) 34 | { 35 | Console.WriteLine("DoWork thread pool thread id: {0}", Thread.CurrentThread.ManagedThreadId); 36 | var bw = (BackgroundWorker) sender; 37 | for (int i = 1; i <= 100; i++) 38 | { 39 | 40 | if (bw.CancellationPending) 41 | { 42 | e.Cancel = true; 43 | return; 44 | } 45 | 46 | if (i%10 == 0) 47 | { 48 | bw.ReportProgress(i); 49 | } 50 | 51 | Thread.Sleep(TimeSpan.FromSeconds(0.1)); 52 | } 53 | e.Result = 42; 54 | } 55 | 56 | static void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 57 | { 58 | Console.WriteLine("{0}% completed. Progress thread pool thread id: {1}", e.ProgressPercentage, 59 | Thread.CurrentThread.ManagedThreadId); 60 | } 61 | 62 | static void Worker_Completed(object sender, RunWorkerCompletedEventArgs e) 63 | { 64 | Console.WriteLine("Completed thread pool thread id: {0}", Thread.CurrentThread.ManagedThreadId); 65 | if (e.Error != null) 66 | { 67 | Console.WriteLine("Exception {0} has occured.", e.Error.Message); 68 | } 69 | else if (e.Cancelled) 70 | { 71 | Console.WriteLine("Operation has been canceled."); 72 | } 73 | else 74 | { 75 | Console.WriteLine("The answer is: {0}", e.Result); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Chapter3/Recipe7/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe7")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe7")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d8017f66-6f29-462c-8f8e-59148d9aa447")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe1/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Chapter4.Recipe1 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var t1 = new Task(() => TaskMethod("Task 1")); 12 | var t2 = new Task(() => TaskMethod("Task 2")); 13 | t2.Start(); 14 | t1.Start(); 15 | Task.Run(() => TaskMethod("Task 3")); 16 | Task.Factory.StartNew(() => TaskMethod("Task 4")); 17 | Task.Factory.StartNew(() => TaskMethod("Task 5"), TaskCreationOptions.LongRunning); 18 | Thread.Sleep(TimeSpan.FromSeconds(1)); 19 | } 20 | 21 | static void TaskMethod(string name) 22 | { 23 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 24 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chapter4/Recipe1/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe1")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("540b87a9-d995-4f22-b1d8-e72ffd74a205")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe2/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe2/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Chapter4.Recipe2 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | TaskMethod("Main Thread Task"); 12 | Task task = CreateTask("Task 1"); 13 | task.Start(); 14 | int result = task.Result; 15 | Console.WriteLine("Result is: {0}", result); 16 | 17 | task = CreateTask("Task 2"); 18 | task.RunSynchronously(); 19 | result = task.Result; 20 | Console.WriteLine("Result is: {0}", result); 21 | 22 | task = CreateTask("Task 3"); 23 | Console.WriteLine(task.Status); 24 | task.Start(); 25 | 26 | while (!task.IsCompleted) 27 | { 28 | Console.WriteLine(task.Status); 29 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 30 | } 31 | 32 | Console.WriteLine(task.Status); 33 | result = task.Result; 34 | Console.WriteLine("Result is: {0}", result); 35 | } 36 | 37 | static Task CreateTask(string name) 38 | { 39 | return new Task(() => TaskMethod(name)); 40 | } 41 | 42 | static int TaskMethod(string name) 43 | { 44 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 45 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 46 | Thread.Sleep(TimeSpan.FromSeconds(2)); 47 | return 42; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Chapter4/Recipe2/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe2")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe2")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("db157e8a-39d7-45b8-9938-a9087f6d4a1a")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe3/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe3/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe3")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("ac88c94e-5c89-41c6-a816-090957c2ac87")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe4/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe4/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe4")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe4")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("ea8dfbb6-94db-4274-bacb-3a4712993fcb")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe5/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe5/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | namespace Chapter4.Recipe5 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var tcs = new TaskCompletionSource(); 13 | 14 | var worker = new BackgroundWorker(); 15 | worker.DoWork += (sender, eventArgs) => 16 | { 17 | eventArgs.Result = TaskMethod("Background worker", 5); 18 | }; 19 | 20 | worker.RunWorkerCompleted += (sender, eventArgs) => 21 | { 22 | if (eventArgs.Error != null) 23 | { 24 | tcs.SetException(eventArgs.Error); 25 | } 26 | else if (eventArgs.Cancelled) 27 | { 28 | tcs.SetCanceled(); 29 | } 30 | else 31 | { 32 | tcs.SetResult((int)eventArgs.Result); 33 | } 34 | }; 35 | 36 | worker.RunWorkerAsync(); 37 | 38 | int result = tcs.Task.Result; 39 | 40 | Console.WriteLine("Result is: {0}", result); 41 | } 42 | 43 | static int TaskMethod(string name, int seconds) 44 | { 45 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 46 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 47 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 48 | return 42 * seconds; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Chapter4/Recipe5/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe5")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe5")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("36711db7-2242-4a17-8332-a8be103a94bb")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe6/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe6/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Chapter4.Recipe6 6 | { 7 | class Program 8 | { 9 | private static void Main(string[] args) 10 | { 11 | var cts = new CancellationTokenSource(); 12 | var longTask = new Task(() => TaskMethod("Task 1", 10, cts.Token), cts.Token); 13 | Console.WriteLine(longTask.Status); 14 | cts.Cancel(); 15 | Console.WriteLine(longTask.Status); 16 | Console.WriteLine("First task has been cancelled before execution"); 17 | cts = new CancellationTokenSource(); 18 | longTask = new Task(() => TaskMethod("Task 2", 10, cts.Token), cts.Token); 19 | longTask.Start(); 20 | for (int i = 0; i < 5; i++ ) 21 | { 22 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 23 | Console.WriteLine(longTask.Status); 24 | } 25 | cts.Cancel(); 26 | for (int i = 0; i < 5; i++) 27 | { 28 | Thread.Sleep(TimeSpan.FromSeconds(0.5)); 29 | Console.WriteLine(longTask.Status); 30 | } 31 | 32 | Console.WriteLine("A task has been completed with result {0}.", longTask.Result); 33 | } 34 | 35 | private static int TaskMethod(string name, int seconds, CancellationToken token) 36 | { 37 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 38 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 39 | for (int i = 0; i < seconds; i ++) 40 | { 41 | Thread.Sleep(TimeSpan.FromSeconds(1)); 42 | if (token.IsCancellationRequested) return -1; 43 | } 44 | return 42*seconds; 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Chapter4/Recipe6/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe6")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe6")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("8b37495d-c8c5-458c-85c3-4fdb1759f1fa")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe7/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe7/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Chapter4.Recipe7 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Task task; 12 | try 13 | { 14 | task = Task.Run(() => TaskMethod("Task 1", 2)); 15 | int result = task.Result; 16 | Console.WriteLine("Result: {0}", result); 17 | } 18 | catch (Exception ex) 19 | { 20 | Console.WriteLine("Exception caught: {0}", ex); 21 | } 22 | Console.WriteLine("----------------------------------------------"); 23 | Console.WriteLine(); 24 | 25 | try 26 | { 27 | task = Task.Run(() => TaskMethod("Task 2", 2)); 28 | int result = task.GetAwaiter().GetResult(); 29 | Console.WriteLine("Result: {0}", result); 30 | } 31 | catch (Exception ex) 32 | { 33 | Console.WriteLine("Exception caught: {0}", ex); 34 | } 35 | Console.WriteLine("----------------------------------------------"); 36 | Console.WriteLine(); 37 | 38 | var t1 = new Task(() => TaskMethod("Task 3", 3)); 39 | var t2 = new Task(() => TaskMethod("Task 4", 2)); 40 | var complexTask = Task.WhenAll(t1, t2); 41 | var exceptionHandler = complexTask.ContinueWith(t => 42 | Console.WriteLine("Exception caught: {0}", t.Exception), 43 | TaskContinuationOptions.OnlyOnFaulted 44 | ); 45 | t1.Start(); 46 | t2.Start(); 47 | 48 | Thread.Sleep(TimeSpan.FromSeconds(5)); 49 | } 50 | 51 | static int TaskMethod(string name, int seconds) 52 | { 53 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 54 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 55 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 56 | throw new Exception("Boom!"); 57 | return 42 * seconds; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Chapter4/Recipe7/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe7")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe7")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("018efd13-0ef9-44ae-beca-6ad1ded1cd3a")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe8/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe8/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | namespace Chapter4.Recipe8 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var firstTask = new Task(() => TaskMethod("First Task", 3)); 13 | var secondTask = new Task(() => TaskMethod("Second Task", 2)); 14 | var whenAllTask = Task.WhenAll(firstTask, secondTask); 15 | 16 | whenAllTask.ContinueWith(t => 17 | Console.WriteLine("The first answer is {0}, the second is {1}", t.Result[0], t.Result[1]), 18 | TaskContinuationOptions.OnlyOnRanToCompletion 19 | ); 20 | 21 | firstTask.Start(); 22 | secondTask.Start(); 23 | 24 | Thread.Sleep(TimeSpan.FromSeconds(4)); 25 | 26 | var tasks = new List>(); 27 | for (int i = 1; i < 4; i++) 28 | { 29 | int counter = i; 30 | var task = new Task(() => TaskMethod(string.Format("Task {0}", counter), counter)); 31 | tasks.Add(task); 32 | task.Start(); 33 | } 34 | 35 | while (tasks.Count > 0) 36 | { 37 | var completedTask = Task.WhenAny(tasks).Result; 38 | tasks.Remove(completedTask); 39 | Console.WriteLine("A task has been completed with result {0}.", completedTask.Result); 40 | } 41 | 42 | Thread.Sleep(TimeSpan.FromSeconds(1)); 43 | } 44 | 45 | static int TaskMethod(string name, int seconds) 46 | { 47 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}", 48 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); 49 | Thread.Sleep(TimeSpan.FromSeconds(seconds)); 50 | return 42 * seconds; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Chapter4/Recipe8/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Recipe8")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Recipe8")] 13 | [assembly: AssemblyCopyright("Copyright © 2013")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f066e26e-90b8-4fde-ad3c-384a787292ae")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Chapter4/Recipe9/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Chapter4/Recipe9/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter4/Recipe9/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace Chapter4.Recipe9 4 | { 5 | /// 6 | /// Interaction logic for App.xaml 7 | /// 8 | public partial class App : Application 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter4/Recipe9/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 |