├── .gitattributes ├── .gitignore ├── BookSamples_VisualStudio2015.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 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe1.csproj ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe3.csproj │ └── packages.config └── Recipe4 │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Recipe4.csproj │ └── packages.config ├── Chapter11 ├── Recipe1 │ ├── App.xaml │ ├── App.xaml.cs │ ├── ApplicationInsights.config │ ├── Assets │ │ ├── LockScreenLogo.scale-200.png │ │ ├── SplashScreen.scale-200.png │ │ ├── Square150x150Logo.scale-200.png │ │ ├── Square44x44Logo.scale-200.png │ │ ├── Square44x44Logo.targetsize-24_altform-unplated.png │ │ ├── StoreLogo.png │ │ └── Wide310x150Logo.scale-200.png │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Package.appxmanifest │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ └── Default.rd.xml │ ├── Recipe1.csproj │ ├── Recipe1_TemporaryKey.pfx │ └── project.json ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Recipe2.csproj ├── Recipe3 │ ├── App.xaml │ ├── App.xaml.cs │ ├── ApplicationInsights.config │ ├── Assets │ │ ├── LockScreenLogo.scale-200.png │ │ ├── SplashScreen.scale-200.png │ │ ├── Square150x150Logo.scale-200.png │ │ ├── Square44x44Logo.scale-200.png │ │ ├── Square44x44Logo.targetsize-24_altform-unplated.png │ │ ├── StoreLogo.png │ │ └── Wide310x150Logo.scale-200.png │ ├── MainPage.xaml │ ├── MainPage.xaml.cs │ ├── Package.appxmanifest │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ └── Default.rd.xml │ ├── Recipe3.csproj │ ├── Recipe3_TemporaryKey.pfx │ └── project.json ├── Recipe4 │ ├── Program.cs │ └── dotnetcore_installation.txt └── Recipe5 │ ├── Program.cs │ └── dotnetcore_installation.txt ├── 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 │ ├── 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 │ ├── 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 │ ├── 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 │ └── packages.config ├── Chapter6 ├── 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 ├── Chapter7 ├── 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 ├── Chapter8 ├── Recipe1 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe1.csproj │ └── packages.config ├── Recipe2 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe2.csproj │ └── packages.config ├── Recipe3 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe3.csproj │ └── packages.config ├── Recipe4 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe4.csproj │ └── packages.config ├── Recipe5 │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Recipe5.csproj │ └── packages.config └── Recipe6 │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Recipe6.csproj │ └── packages.config ├── Chapter9 ├── 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 ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter1/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using static System.Console; 4 | 5 | namespace Chapter1.Recipe1 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | Thread t = new Thread(PrintNumbers); 12 | t.Start(); 13 | PrintNumbers(); 14 | } 15 | 16 | static void PrintNumbers() 17 | { 18 | WriteLine("Starting..."); 19 | for (int i = 1; i < 10; i++) 20 | { 21 | WriteLine(i); 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe10 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | object lock1 = new object(); 13 | object lock2 = new object(); 14 | 15 | new Thread(() => LockTooMuch(lock1, lock2)).Start(); 16 | 17 | lock (lock2) 18 | { 19 | Thread.Sleep(1000); 20 | WriteLine("Monitor.TryEnter allows not to get stuck, returning false after a specified timeout is elapsed"); 21 | if (Monitor.TryEnter(lock1, TimeSpan.FromSeconds(5))) 22 | { 23 | WriteLine("Acquired a protected resource succesfully"); 24 | } 25 | else 26 | { 27 | WriteLine("Timeout acquiring a resource!"); 28 | } 29 | } 30 | 31 | new Thread(() => LockTooMuch(lock1, lock2)).Start(); 32 | 33 | WriteLine("----------------------------------"); 34 | lock (lock2) 35 | { 36 | WriteLine("This will be a deadlock!"); 37 | Sleep(1000); 38 | lock (lock1) 39 | { 40 | WriteLine("Acquired a protected resource succesfully"); 41 | } 42 | } 43 | } 44 | 45 | static void LockTooMuch(object lock1, object lock2) 46 | { 47 | lock (lock1) 48 | { 49 | Sleep(1000); 50 | lock (lock2); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe11 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t = new Thread(FaultyThread); 13 | t.Start(); 14 | t.Join(); 15 | 16 | try 17 | { 18 | t = new Thread(BadFaultyThread); 19 | t.Start(); 20 | } 21 | catch (Exception ex) 22 | { 23 | WriteLine("We won't get here!"); 24 | } 25 | } 26 | 27 | static void BadFaultyThread() 28 | { 29 | WriteLine("Starting a faulty thread..."); 30 | Sleep(TimeSpan.FromSeconds(2)); 31 | throw new Exception("Boom!"); 32 | } 33 | 34 | static void FaultyThread() 35 | { 36 | try 37 | { 38 | WriteLine("Starting a faulty thread..."); 39 | Sleep(TimeSpan.FromSeconds(1)); 40 | throw new Exception("Boom!"); 41 | } 42 | catch (Exception ex) 43 | { 44 | WriteLine($"Exception handled: {ex.Message}"); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe2 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | Thread t = new Thread(PrintNumbersWithDelay); 13 | t.Start(); 14 | PrintNumbers(); 15 | } 16 | 17 | static void PrintNumbers() 18 | { 19 | WriteLine("Starting..."); 20 | for (int i = 1; i < 10; i++) 21 | { 22 | WriteLine(i); 23 | } 24 | } 25 | 26 | static void PrintNumbersWithDelay() 27 | { 28 | WriteLine("Starting..."); 29 | for (int i = 1; i < 10; i++) 30 | { 31 | Sleep(TimeSpan.FromSeconds(2)); 32 | WriteLine(i); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe3 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | WriteLine("Starting program..."); 13 | Thread t = new Thread(PrintNumbersWithDelay); 14 | t.Start(); 15 | t.Join(); 16 | WriteLine("Thread completed"); 17 | } 18 | 19 | static void PrintNumbersWithDelay() 20 | { 21 | WriteLine("Starting..."); 22 | for (int i = 1; i < 10; i++) 23 | { 24 | Sleep(TimeSpan.FromSeconds(2)); 25 | WriteLine(i); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe4 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | WriteLine("Starting program..."); 13 | Thread t = new Thread(PrintNumbersWithDelay); 14 | t.Start(); 15 | Sleep(TimeSpan.FromSeconds(6)); 16 | t.Abort(); 17 | WriteLine("A thread has been aborted"); 18 | } 19 | 20 | static void PrintNumbersWithDelay() 21 | { 22 | WriteLine("Starting..."); 23 | for (int i = 1; i < 10; i++) 24 | { 25 | Sleep(TimeSpan.FromSeconds(2)); 26 | WriteLine(i); 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe5 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | WriteLine("Starting program..."); 13 | Thread t = new Thread(PrintNumbersWithStatus); 14 | Thread t2 = new Thread(DoNothing); 15 | WriteLine(t.ThreadState.ToString()); 16 | t2.Start(); 17 | t.Start(); 18 | for (int i = 1; i < 30; i++) 19 | { 20 | WriteLine(t.ThreadState.ToString()); 21 | } 22 | Sleep(TimeSpan.FromSeconds(6)); 23 | t.Abort(); 24 | WriteLine("A thread has been aborted"); 25 | WriteLine(t.ThreadState.ToString()); 26 | WriteLine(t2.ThreadState.ToString()); 27 | } 28 | 29 | static void DoNothing() 30 | { 31 | Sleep(TimeSpan.FromSeconds(2)); 32 | } 33 | 34 | static void PrintNumbersWithStatus() 35 | { 36 | WriteLine("Starting..."); 37 | WriteLine(CurrentThread.ThreadState.ToString()); 38 | for (int i = 1; i < 10; i++) 39 | { 40 | Sleep(TimeSpan.FromSeconds(2)); 41 | WriteLine(i); 42 | } 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /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.Threading; 3 | using static System.Console; 4 | using static System.Threading.Thread; 5 | using static System.Diagnostics.Process; 6 | 7 | namespace Chapter1.Recipe6 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | WriteLine($"Current thread priority: {CurrentThread.Priority}"); 14 | WriteLine("Running on all cores available"); 15 | RunThreads(); 16 | Sleep(TimeSpan.FromSeconds(2)); 17 | WriteLine("Running on a single core"); 18 | GetCurrentProcess().ProcessorAffinity = new IntPtr(1); 19 | RunThreads(); 20 | } 21 | 22 | static void RunThreads() 23 | { 24 | var sample = new ThreadSample(); 25 | 26 | var threadOne = new Thread(sample.CountNumbers); 27 | threadOne.Name = "ThreadOne"; 28 | var threadTwo = new Thread(sample.CountNumbers); 29 | threadTwo.Name = "ThreadTwo"; 30 | 31 | threadOne.Priority = ThreadPriority.Highest; 32 | threadTwo.Priority = ThreadPriority.Lowest; 33 | threadOne.Start(); 34 | threadTwo.Start(); 35 | 36 | Sleep(TimeSpan.FromSeconds(2)); 37 | sample.Stop(); 38 | } 39 | 40 | class ThreadSample 41 | { 42 | private bool _isStopped = false; 43 | 44 | public void Stop() 45 | { 46 | _isStopped = true; 47 | } 48 | 49 | public void CountNumbers() 50 | { 51 | long counter = 0; 52 | 53 | while (!_isStopped) 54 | { 55 | counter++; 56 | } 57 | 58 | WriteLine($"{CurrentThread.Name} with " + 59 | $"{CurrentThread.Priority,11} priority " + 60 | $"has a count = {counter,13:N0}"); 61 | } 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe7 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var sampleForeground = new ThreadSample(10); 13 | var sampleBackground = new ThreadSample(20); 14 | 15 | var threadOne = new Thread(sampleForeground.CountNumbers); 16 | threadOne.Name = "ForegroundThread"; 17 | var threadTwo = new Thread(sampleBackground.CountNumbers); 18 | threadTwo.Name = "BackgroundThread"; 19 | threadTwo.IsBackground = true; 20 | 21 | threadOne.Start(); 22 | threadTwo.Start(); 23 | } 24 | 25 | class ThreadSample 26 | { 27 | private readonly int _iterations; 28 | 29 | public ThreadSample(int iterations) 30 | { 31 | _iterations = iterations; 32 | } 33 | public void CountNumbers() 34 | { 35 | for (int i = 0; i < _iterations; i++) 36 | { 37 | Sleep(TimeSpan.FromSeconds(0.5)); 38 | WriteLine($"{CurrentThread.Name} prints {i}"); 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter1.Recipe8 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var sample = new ThreadSample(10); 13 | 14 | var threadOne = new Thread(sample.CountNumbers); 15 | threadOne.Name = "ThreadOne"; 16 | threadOne.Start(); 17 | threadOne.Join(); 18 | 19 | WriteLine("--------------------------"); 20 | 21 | var threadTwo = new Thread(Count); 22 | threadTwo.Name = "ThreadTwo"; 23 | threadTwo.Start(8); 24 | threadTwo.Join(); 25 | 26 | WriteLine("--------------------------"); 27 | 28 | var threadThree = new Thread(() => CountNumbers(12)); 29 | threadThree.Name = "ThreadThree"; 30 | threadThree.Start(); 31 | threadThree.Join(); 32 | WriteLine("--------------------------"); 33 | 34 | int i = 10; 35 | var threadFour = new Thread(() => PrintNumber(i)); 36 | i = 20; 37 | var threadFive = new Thread(() => PrintNumber(i)); 38 | threadFour.Start(); 39 | threadFive.Start(); 40 | } 41 | 42 | static void Count(object iterations) 43 | { 44 | CountNumbers((int)iterations); 45 | } 46 | 47 | static void CountNumbers(int iterations) 48 | { 49 | for (int i = 1; i <= iterations; i++) 50 | { 51 | Sleep(TimeSpan.FromSeconds(0.5)); 52 | WriteLine($"{CurrentThread.Name} prints {i}"); 53 | } 54 | } 55 | 56 | static void PrintNumber(int number) 57 | { 58 | WriteLine(number); 59 | } 60 | 61 | class ThreadSample 62 | { 63 | private readonly int _iterations; 64 | 65 | public ThreadSample(int iterations) 66 | { 67 | _iterations = iterations; 68 | } 69 | public void CountNumbers() 70 | { 71 | for (int i = 1; i <= _iterations; i++) 72 | { 73 | Sleep(TimeSpan.FromSeconds(0.5)); 74 | WriteLine($"{CurrentThread.Name} prints {i}"); 75 | } 76 | } 77 | } 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using static System.Console; 4 | 5 | namespace Chapter1.Recipe9 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | WriteLine("Incorrect counter"); 12 | 13 | var c = new Counter(); 14 | 15 | var t1 = new Thread(() => TestCounter(c)); 16 | var t2 = new Thread(() => TestCounter(c)); 17 | var t3 = new Thread(() => TestCounter(c)); 18 | t1.Start(); 19 | t2.Start(); 20 | t3.Start(); 21 | t1.Join(); 22 | t2.Join(); 23 | t3.Join(); 24 | 25 | WriteLine($"Total count: {c.Count}"); 26 | WriteLine("--------------------------"); 27 | 28 | WriteLine("Correct counter"); 29 | 30 | var c1 = new CounterWithLock(); 31 | 32 | t1 = new Thread(() => TestCounter(c1)); 33 | t2 = new Thread(() => TestCounter(c1)); 34 | t3 = new Thread(() => TestCounter(c1)); 35 | t1.Start(); 36 | t2.Start(); 37 | t3.Start(); 38 | t1.Join(); 39 | t2.Join(); 40 | t3.Join(); 41 | WriteLine($"Total count: {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 | public int Count { get; private set; } 56 | 57 | public override void Increment() 58 | { 59 | Count++; 60 | } 61 | 62 | public override void Decrement() 63 | { 64 | Count--; 65 | } 66 | } 67 | 68 | class CounterWithLock : CounterBase 69 | { 70 | private readonly object _syncRoot = new Object(); 71 | 72 | public int Count { get; private set; } 73 | 74 | public override void Increment() 75 | { 76 | lock (_syncRoot) 77 | { 78 | Count++; 79 | } 80 | } 81 | 82 | public override void Decrement() 83 | { 84 | lock (_syncRoot) 85 | { 86 | Count--; 87 | } 88 | } 89 | } 90 | 91 | abstract class CounterBase 92 | { 93 | public abstract void Increment(); 94 | 95 | public abstract void Decrement(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /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/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/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/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/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 | -------------------------------------------------------------------------------- /Chapter10/Recipe4/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/App.xaml: -------------------------------------------------------------------------------- 1 |  7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/ApplicationInsights.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/LockScreenLogo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/LockScreenLogo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/SplashScreen.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/SplashScreen.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/Square150x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square150x150Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/Square44x44Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square44x44Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/Square44x44Logo.targetsize-24_altform-unplated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square44x44Logo.targetsize-24_altform-unplated.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/StoreLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/Assets/Wide310x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Wide310x150Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe1/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 8 | 9 | 13 | 14 | 15 | 16 | 17 | Recipe1 18 | vagrant 19 | Assets\StoreLogo.png 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 34 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /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("Vagrant")] 12 | [assembly: AssemblyProduct("Recipe1")] 13 | [assembly: AssemblyCopyright("Copyright © Vagrant 2016")] 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/Recipe1/Properties/Default.rd.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/Recipe1/Recipe1_TemporaryKey.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Recipe1_TemporaryKey.pfx -------------------------------------------------------------------------------- /Chapter11/Recipe1/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "Microsoft.ApplicationInsights": "1.0.0", 4 | "Microsoft.ApplicationInsights.PersistenceChannel": "1.0.0", 5 | "Microsoft.ApplicationInsights.WindowsApps": "1.0.0", 6 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0" 7 | }, 8 | "frameworks": { 9 | "uap10.0": {} 10 | }, 11 | "runtimes": { 12 | "win10-arm": {}, 13 | "win10-arm-aot": {}, 14 | "win10-x86": {}, 15 | "win10-x86-aot": {}, 16 | "win10-x64": {}, 17 | "win10-x64-aot": {} 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter11/Recipe2/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Threading.Tasks; 4 | using Windows.Storage; 5 | 6 | namespace Chapter11.Recipe2 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t = AsynchronousProcessing(); 13 | t.GetAwaiter().GetResult(); 14 | Console.WriteLine(); 15 | Console.WriteLine("Press ENTER to continue"); 16 | Console.ReadLine(); 17 | } 18 | 19 | async static Task AsynchronousProcessing() 20 | { 21 | StorageFolder folder = KnownFolders.DocumentsLibrary; 22 | 23 | if (await folder.DoesFileExistAsync("test.txt")) 24 | { 25 | var fileToDelete = await folder.GetFileAsync("test.txt"); 26 | await fileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete); 27 | } 28 | 29 | var file = await folder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting); 30 | using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite)) 31 | using (var writer = new StreamWriter(stream.AsStreamForWrite())) 32 | { 33 | await writer.WriteLineAsync("Test content"); 34 | await writer.FlushAsync(); 35 | } 36 | 37 | using (var stream = await file.OpenAsync(FileAccessMode.Read)) 38 | using (var reader = new StreamReader(stream.AsStreamForRead())) 39 | { 40 | string content = await reader.ReadToEndAsync(); 41 | Console.WriteLine(content); 42 | } 43 | 44 | Console.WriteLine("Enumerating Folder Structure:"); 45 | 46 | var itemsList = await folder.GetItemsAsync(); 47 | foreach (var item in itemsList) 48 | { 49 | if (item is StorageFolder) 50 | { 51 | Console.WriteLine("{0} folder", item.Name); 52 | } 53 | else 54 | { 55 | Console.WriteLine(item.Name); 56 | } 57 | } 58 | } 59 | } 60 | 61 | static class Extensions 62 | { 63 | public static async Task DoesFileExistAsync(this StorageFolder folder, string fileName) 64 | { 65 | try 66 | { 67 | await folder.GetFileAsync(fileName); 68 | return true; 69 | } 70 | catch (FileNotFoundException) 71 | { 72 | return false; 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /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 |  7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/ApplicationInsights.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/LockScreenLogo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/LockScreenLogo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/SplashScreen.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/SplashScreen.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/Square150x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square150x150Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/Square44x44Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square44x44Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/Square44x44Logo.targetsize-24_altform-unplated.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square44x44Logo.targetsize-24_altform-unplated.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/StoreLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/StoreLogo.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/Assets/Wide310x150Logo.scale-200.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Wide310x150Logo.scale-200.png -------------------------------------------------------------------------------- /Chapter11/Recipe3/MainPage.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Package.appxmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Recipe3 7 | vagrant 8 | Assets\StoreLogo.png 9 | 10 | 11 | 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 | -------------------------------------------------------------------------------- /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("Vagrant")] 12 | [assembly: AssemblyProduct("Recipe3")] 13 | [assembly: AssemblyCopyright("Copyright © Vagrant 2016")] 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/Recipe3/Properties/Default.rd.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 19 | 20 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Chapter11/Recipe3/Recipe3_TemporaryKey.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Recipe3_TemporaryKey.pfx -------------------------------------------------------------------------------- /Chapter11/Recipe3/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "Microsoft.ApplicationInsights": "1.0.0", 4 | "Microsoft.ApplicationInsights.PersistenceChannel": "1.0.0", 5 | "Microsoft.ApplicationInsights.WindowsApps": "1.0.0", 6 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0" 7 | }, 8 | "frameworks": { 9 | "uap10.0": {} 10 | }, 11 | "runtimes": { 12 | "win10-arm": {}, 13 | "win10-arm-aot": {}, 14 | "win10-x86": {}, 15 | "win10-x86-aot": {}, 16 | "win10-x64": {}, 17 | "win10-x64-aot": {} 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter11/Recipe4/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using static System.Console; 4 | 5 | namespace OSXConsoleApplication 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | WriteLine(".NET Core app on OS X"); 12 | RunCodeAsync().GetAwaiter().GetResult(); 13 | } 14 | 15 | static async Task RunCodeAsync() 16 | { 17 | try 18 | { 19 | string result = await GetInfoAsync("Async 1"); 20 | WriteLine(result); 21 | result = await GetInfoAsync("Async 2"); 22 | WriteLine(result); 23 | } 24 | catch (Exception ex) 25 | { 26 | WriteLine(ex); 27 | } 28 | } 29 | 30 | static async Task GetInfoAsync(string name) 31 | { 32 | WriteLine($"Task {name} started!"); 33 | await Task.Delay(TimeSpan.FromSeconds(2)); 34 | if(name == "Async 2") 35 | throw new Exception("Boom!"); 36 | return 37 | $"Task {name} completed successfully!" 38 | // + $"Thread id {System.Threading.Thread.CurrentThread.ManagedThreadId}." 39 | ; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Chapter11/Recipe4/dotnetcore_installation.txt: -------------------------------------------------------------------------------- 1 | #please do it on your own risk! 2 | sudo sysctl -w kern.maxfiles=20480 3 | 4 | sudo sysctl -w kern.maxfilesperproc=18000 5 | 6 | sudo ulimit -S -n 2048 7 | 8 | mkdir HelloWorld 9 | 10 | cd HelloWorld 11 | 12 | dotnet new 13 | 14 | dotnet restore 15 | 16 | dotnet run 17 | -------------------------------------------------------------------------------- /Chapter11/Recipe5/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using static System.Console; 4 | 5 | namespace OSXConsoleApplication 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | WriteLine(".NET Core app on Ubuntu"); 12 | RunCodeAsync().GetAwaiter().GetResult(); 13 | } 14 | 15 | static async Task RunCodeAsync() 16 | { 17 | try 18 | { 19 | string result = await GetInfoAsync("Async 1"); 20 | WriteLine(result); 21 | result = await GetInfoAsync("Async 2"); 22 | WriteLine(result); 23 | } 24 | catch (Exception ex) 25 | { 26 | WriteLine(ex); 27 | } 28 | } 29 | 30 | static async Task GetInfoAsync(string name) 31 | { 32 | WriteLine($"Task {name} started!"); 33 | await Task.Delay(TimeSpan.FromSeconds(2)); 34 | if(name == "Async 2") 35 | throw new Exception("Boom!"); 36 | return 37 | $"Task {name} completed successfully!" 38 | // + $"Thread id {System.Threading.Thread.CurrentThread.ManagedThreadId}." 39 | ; 40 | } 41 | } 42 | } -------------------------------------------------------------------------------- /Chapter11/Recipe5/dotnetcore_installation.txt: -------------------------------------------------------------------------------- 1 | sudo sh -c 'echo "deb [arch=amd64] http://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list' 2 | 3 | sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893 4 | 5 | sudo apt-get update 6 | 7 | mkdir HelloWorld 8 | 9 | cd HelloWorld 10 | 11 | dotnet new 12 | 13 | dotnet restore 14 | 15 | dotnet run -------------------------------------------------------------------------------- /Chapter2/Recipe1/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using static System.Console; 4 | 5 | namespace Chapter2.Recipe1 6 | { 7 | internal class Program 8 | { 9 | private static void Main(string[] args) 10 | { 11 | WriteLine("Incorrect counter"); 12 | 13 | var c = new Counter(); 14 | 15 | var t1 = new Thread(() => TestCounter(c)); 16 | var t2 = new Thread(() => TestCounter(c)); 17 | var t3 = new Thread(() => TestCounter(c)); 18 | t1.Start(); 19 | t2.Start(); 20 | t3.Start(); 21 | t1.Join(); 22 | t2.Join(); 23 | t3.Join(); 24 | 25 | WriteLine($"Total count: {c.Count}"); 26 | WriteLine("--------------------------"); 27 | 28 | WriteLine("Correct counter"); 29 | 30 | var c1 = new CounterNoLock(); 31 | 32 | t1 = new Thread(() => TestCounter(c1)); 33 | t2 = new Thread(() => TestCounter(c1)); 34 | t3 = new Thread(() => TestCounter(c1)); 35 | t1.Start(); 36 | t2.Start(); 37 | t3.Start(); 38 | t1.Join(); 39 | t2.Join(); 40 | t3.Join(); 41 | 42 | WriteLine($"Total count: {c1.Count}"); 43 | } 44 | 45 | static void TestCounter(CounterBase c) 46 | { 47 | for (int i = 0; i < 100000; i++) 48 | { 49 | c.Increment(); 50 | c.Decrement(); 51 | } 52 | } 53 | 54 | class Counter : CounterBase 55 | { 56 | private int _count; 57 | 58 | public int Count => _count; 59 | 60 | public override void Increment() 61 | { 62 | _count++; 63 | } 64 | 65 | public override void Decrement() 66 | { 67 | _count--; 68 | } 69 | } 70 | 71 | class CounterNoLock : CounterBase 72 | { 73 | private int _count; 74 | 75 | public int Count => _count; 76 | 77 | public override void Increment() 78 | { 79 | Interlocked.Increment(ref _count); 80 | } 81 | 82 | public override void Decrement() 83 | { 84 | Interlocked.Decrement(ref _count); 85 | } 86 | } 87 | 88 | abstract class CounterBase 89 | { 90 | public abstract void Increment(); 91 | 92 | public abstract void Decrement(); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | 5 | namespace Chapter2.Recipe2 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | const string MutexName = "CSharpThreadingCookbook"; 12 | 13 | using (var m = new Mutex(false, MutexName)) 14 | { 15 | if (!m.WaitOne(TimeSpan.FromSeconds(5), false)) 16 | { 17 | WriteLine("Second instance is running!"); 18 | } 19 | else 20 | { 21 | WriteLine("Running!"); 22 | ReadLine(); 23 | m.ReleaseMutex(); 24 | } 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe3 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | for (int i = 1; i <= 6; i++) 13 | { 14 | string threadName = "Thread " + i; 15 | int secondsToWait = 2 + 2 * i; 16 | var t = new Thread(() => AccessDatabase(threadName, secondsToWait)); 17 | t.Start(); 18 | } 19 | } 20 | 21 | static SemaphoreSlim _semaphore = new SemaphoreSlim(4); 22 | 23 | static void AccessDatabase(string name, int seconds) 24 | { 25 | WriteLine($"{name} waits to access a database"); 26 | _semaphore.Wait(); 27 | WriteLine($"{name} was granted an access to a database"); 28 | Sleep(TimeSpan.FromSeconds(seconds)); 29 | WriteLine($"{name} is completed"); 30 | _semaphore.Release(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe4 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t = new Thread(() => Process(10)); 13 | t.Start(); 14 | 15 | WriteLine("Waiting for another thread to complete work"); 16 | _workerEvent.WaitOne(); 17 | WriteLine("First operation is completed!"); 18 | WriteLine("Performing an operation on a main thread"); 19 | Sleep(TimeSpan.FromSeconds(5)); 20 | _mainEvent.Set(); 21 | WriteLine("Now running the second operation on a second thread"); 22 | _workerEvent.WaitOne(); 23 | WriteLine("Second operation is completed!"); 24 | } 25 | 26 | private static AutoResetEvent _workerEvent = new AutoResetEvent(false); 27 | private static AutoResetEvent _mainEvent = new AutoResetEvent(false); 28 | 29 | static void Process(int seconds) 30 | { 31 | WriteLine("Starting a long running work..."); 32 | Sleep(TimeSpan.FromSeconds(seconds)); 33 | WriteLine("Work is done!"); 34 | _workerEvent.Set(); 35 | WriteLine("Waiting for a main thread to complete its work"); 36 | _mainEvent.WaitOne(); 37 | WriteLine("Starting second operation..."); 38 | Sleep(TimeSpan.FromSeconds(seconds)); 39 | WriteLine("Work is done!"); 40 | _workerEvent.Set(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe5 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t1 = new Thread(() => TravelThroughGates("Thread 1", 5)); 13 | var t2 = new Thread(() => TravelThroughGates("Thread 2", 6)); 14 | var t3 = new Thread(() => TravelThroughGates("Thread 3", 12)); 15 | t1.Start(); 16 | t2.Start(); 17 | t3.Start(); 18 | Sleep(TimeSpan.FromSeconds(6)); 19 | WriteLine("The gates are now open!"); 20 | _mainEvent.Set(); 21 | Sleep(TimeSpan.FromSeconds(2)); 22 | _mainEvent.Reset(); 23 | WriteLine("The gates have been closed!"); 24 | Sleep(TimeSpan.FromSeconds(10)); 25 | WriteLine("The gates are now open for the second time!"); 26 | _mainEvent.Set(); 27 | Sleep(TimeSpan.FromSeconds(2)); 28 | WriteLine("The gates have been closed!"); 29 | _mainEvent.Reset(); 30 | } 31 | 32 | static void TravelThroughGates(string threadName, int seconds) 33 | { 34 | WriteLine($"{threadName} falls to sleep"); 35 | Sleep(TimeSpan.FromSeconds(seconds)); 36 | WriteLine($"{threadName} waits for the gates to open!"); 37 | _mainEvent.Wait(); 38 | WriteLine($"{threadName} enters the gates!"); 39 | } 40 | 41 | static ManualResetEventSlim _mainEvent = new ManualResetEventSlim(false); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe6 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | WriteLine("Starting two operations"); 13 | var t1 = new Thread(() => PerformOperation("Operation 1 is completed", 4)); 14 | var t2 = new Thread(() => PerformOperation("Operation 2 is completed", 8)); 15 | t1.Start(); 16 | t2.Start(); 17 | _countdown.Wait(); 18 | WriteLine("Both operations have been completed."); 19 | _countdown.Dispose(); 20 | } 21 | 22 | static CountdownEvent _countdown = new CountdownEvent(2); 23 | 24 | static void PerformOperation(string message, int seconds) 25 | { 26 | Sleep(TimeSpan.FromSeconds(seconds)); 27 | WriteLine(message); 28 | _countdown.Signal(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe7 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t1 = new Thread(() => PlayMusic("the guitarist", "play an amazing solo", 5)); 13 | var t2 = new Thread(() => PlayMusic("the singer", "sing his song", 2)); 14 | 15 | t1.Start(); 16 | t2.Start(); 17 | } 18 | 19 | static Barrier _barrier = new Barrier(2, 20 | b => WriteLine($"End of phase {b.CurrentPhaseNumber + 1}")); 21 | 22 | static void PlayMusic(string name, string message, int seconds) 23 | { 24 | for (int i = 1; i < 3; i++) 25 | { 26 | WriteLine("----------------------------------------------"); 27 | Sleep(TimeSpan.FromSeconds(seconds)); 28 | WriteLine($"{name} starts to {message}"); 29 | Sleep(TimeSpan.FromSeconds(seconds)); 30 | WriteLine($"{name} finishes to {message}"); 31 | _barrier.SignalAndWait(); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /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 | using static System.Console; 5 | using static System.Threading.Thread; 6 | 7 | namespace Chapter2.Recipe8 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | new Thread(Read){ IsBackground = true }.Start(); 14 | new Thread(Read){ IsBackground = true }.Start(); 15 | new Thread(Read){ IsBackground = true }.Start(); 16 | 17 | new Thread(() => Write("Thread 1")){ IsBackground = true }.Start(); 18 | new Thread(() => Write("Thread 2")){ IsBackground = true }.Start(); 19 | 20 | Sleep(TimeSpan.FromSeconds(30)); 21 | } 22 | 23 | static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim(); 24 | static Dictionary _items = new Dictionary(); 25 | 26 | static void Read() 27 | { 28 | WriteLine("Reading contents of a dictionary"); 29 | while (true) 30 | { 31 | try 32 | { 33 | _rw.EnterReadLock(); 34 | foreach (var key in _items.Keys) 35 | { 36 | Sleep(TimeSpan.FromSeconds(0.1)); 37 | } 38 | } 39 | finally 40 | { 41 | _rw.ExitReadLock(); 42 | } 43 | } 44 | } 45 | 46 | static void Write(string threadName) 47 | { 48 | while (true) 49 | { 50 | try 51 | { 52 | int newKey = new Random().Next(250); 53 | _rw.EnterUpgradeableReadLock(); 54 | if (!_items.ContainsKey(newKey)) 55 | { 56 | try 57 | { 58 | _rw.EnterWriteLock(); 59 | _items[newKey] = 1; 60 | WriteLine($"New key {newKey} is added to a dictionary by a {threadName}"); 61 | } 62 | finally 63 | { 64 | _rw.ExitWriteLock(); 65 | } 66 | } 67 | Sleep(TimeSpan.FromSeconds(0.1)); 68 | } 69 | finally 70 | { 71 | _rw.ExitUpgradeableReadLock(); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter2.Recipe9 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t1 = new Thread(UserModeWait); 13 | var t2 = new Thread(HybridSpinWait); 14 | 15 | WriteLine("Running user mode waiting"); 16 | t1.Start(); 17 | Sleep(20); 18 | _isCompleted = true; 19 | Sleep(TimeSpan.FromSeconds(1)); 20 | _isCompleted = false; 21 | WriteLine("Running hybrid SpinWait construct waiting"); 22 | t2.Start(); 23 | Sleep(5); 24 | _isCompleted = true; 25 | } 26 | 27 | static volatile bool _isCompleted = false; 28 | 29 | static void UserModeWait() 30 | { 31 | while (!_isCompleted) 32 | { 33 | Write("."); 34 | } 35 | WriteLine(); 36 | WriteLine("Waiting is complete"); 37 | } 38 | 39 | static void HybridSpinWait() 40 | { 41 | var w = new SpinWait(); 42 | while (!_isCompleted) 43 | { 44 | w.SpinOnce(); 45 | WriteLine(w.NextSpinWillYield); 46 | } 47 | WriteLine("Waiting is complete"); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter3.Recipe1 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | int threadId = 0; 13 | 14 | RunOnThreadPool poolDelegate = Test; 15 | 16 | var t = new Thread(() => Test(out threadId)); 17 | t.Start(); 18 | t.Join(); 19 | 20 | WriteLine($"Thread id: {threadId}"); 21 | 22 | IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call"); 23 | r.AsyncWaitHandle.WaitOne(); 24 | 25 | string result = poolDelegate.EndInvoke(out threadId, r); 26 | 27 | WriteLine($"Thread pool worker thread id: {threadId}"); 28 | WriteLine(result); 29 | 30 | Sleep(TimeSpan.FromSeconds(2)); 31 | } 32 | 33 | private delegate string RunOnThreadPool(out int threadId); 34 | 35 | private static void Callback(IAsyncResult ar) 36 | { 37 | WriteLine("Starting a callback..."); 38 | WriteLine($"State passed to a callbak: {ar.AsyncState}"); 39 | WriteLine($"Is thread pool thread: {CurrentThread.IsThreadPoolThread}"); 40 | WriteLine($"Thread pool worker thread id: {CurrentThread.ManagedThreadId}"); 41 | } 42 | 43 | 44 | private static string Test(out int threadId) 45 | { 46 | WriteLine("Starting..."); 47 | WriteLine($"Is thread pool thread: {CurrentThread.IsThreadPoolThread}"); 48 | Sleep(TimeSpan.FromSeconds(2)); 49 | threadId = CurrentThread.ManagedThreadId; 50 | return $"Thread pool worker thread id was: {threadId}"; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter3.Recipe2 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | const int x = 1; 13 | const int y = 2; 14 | const string lambdaState = "lambda state 2"; 15 | 16 | ThreadPool.QueueUserWorkItem(AsyncOperation); 17 | Sleep(TimeSpan.FromSeconds(1)); 18 | 19 | ThreadPool.QueueUserWorkItem(AsyncOperation, "async state"); 20 | Sleep(TimeSpan.FromSeconds(1)); 21 | 22 | ThreadPool.QueueUserWorkItem( state => 23 | { 24 | WriteLine($"Operation state: {state}"); 25 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}"); 26 | Sleep(TimeSpan.FromSeconds(2)); 27 | }, "lambda state"); 28 | 29 | ThreadPool.QueueUserWorkItem( _ => 30 | { 31 | WriteLine($"Operation state: {x + y}, {lambdaState}"); 32 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}"); 33 | Sleep(TimeSpan.FromSeconds(2)); 34 | }, "lambda state"); 35 | 36 | Sleep(TimeSpan.FromSeconds(2)); 37 | } 38 | 39 | private static void AsyncOperation(object state) 40 | { 41 | WriteLine($"Operation state: {state ?? "(null)"}"); 42 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}"); 43 | Sleep(TimeSpan.FromSeconds(2)); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /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 | using static System.Console; 5 | using static System.Threading.Thread; 6 | 7 | namespace Chapter3.Recipe3 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | const int numberOfOperations = 500; 14 | var sw = new Stopwatch(); 15 | sw.Start(); 16 | UseThreads(numberOfOperations); 17 | sw.Stop(); 18 | WriteLine($"Execution time using threads: {sw.ElapsedMilliseconds}"); 19 | 20 | sw.Reset(); 21 | sw.Start(); 22 | UseThreadPool(numberOfOperations); 23 | sw.Stop(); 24 | WriteLine($"Execution time using the thread pool: {sw.ElapsedMilliseconds}"); 25 | } 26 | 27 | static void UseThreads(int numberOfOperations) 28 | { 29 | using (var countdown = new CountdownEvent(numberOfOperations)) 30 | { 31 | WriteLine("Scheduling work by creating threads"); 32 | for (int i = 0; i < numberOfOperations; i++) 33 | { 34 | var thread = new Thread(() => 35 | { 36 | Write($"{CurrentThread.ManagedThreadId},"); 37 | Sleep(TimeSpan.FromSeconds(0.1)); 38 | countdown.Signal(); 39 | }); 40 | thread.Start(); 41 | } 42 | countdown.Wait(); 43 | WriteLine(); 44 | } 45 | } 46 | 47 | static void UseThreadPool(int numberOfOperations) 48 | { 49 | using (var countdown = new CountdownEvent(numberOfOperations)) 50 | { 51 | WriteLine("Starting work on a threadpool"); 52 | for (int i = 0; i < numberOfOperations; i++) 53 | { 54 | ThreadPool.QueueUserWorkItem( _ => 55 | { 56 | Write($"{CurrentThread.ManagedThreadId},"); 57 | Sleep(TimeSpan.FromSeconds(0.1)); 58 | countdown.Signal(); 59 | }); 60 | } 61 | countdown.Wait(); 62 | WriteLine(); 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | 7 | namespace Chapter3.Recipe5 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | RunOperations(TimeSpan.FromSeconds(5)); 14 | RunOperations(TimeSpan.FromSeconds(7)); 15 | } 16 | 17 | static void RunOperations(TimeSpan workerOperationTimeout) 18 | { 19 | using (var evt = new ManualResetEvent(false)) 20 | using (var cts = new CancellationTokenSource()) 21 | { 22 | WriteLine("Registering timeout operation..."); 23 | var worker = ThreadPool.RegisterWaitForSingleObject(evt 24 | , (state, isTimedOut) => WorkerOperationWait(cts, isTimedOut) 25 | , null 26 | , workerOperationTimeout 27 | , true); 28 | 29 | WriteLine("Starting long running operation..."); 30 | ThreadPool.QueueUserWorkItem(_ => WorkerOperation(cts.Token, evt)); 31 | 32 | Sleep(workerOperationTimeout.Add(TimeSpan.FromSeconds(2))); 33 | worker.Unregister(evt); 34 | } 35 | } 36 | 37 | static void WorkerOperation(CancellationToken token, ManualResetEvent evt) 38 | { 39 | for(int i = 0; i < 6; i++) 40 | { 41 | if (token.IsCancellationRequested) 42 | { 43 | return; 44 | } 45 | Sleep(TimeSpan.FromSeconds(1)); 46 | } 47 | evt.Set(); 48 | } 49 | 50 | static void WorkerOperationWait(CancellationTokenSource cts, bool isTimedOut) 51 | { 52 | if (isTimedOut) 53 | { 54 | cts.Cancel(); 55 | WriteLine("Worker operation timed out and was canceled."); 56 | } 57 | else 58 | { 59 | WriteLine("Worker operation succeded."); 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /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 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter3.Recipe6 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | WriteLine("Press 'Enter' to stop the timer..."); 13 | DateTime start = DateTime.Now; 14 | _timer = new Timer(_ => TimerOperation(start), null 15 | , TimeSpan.FromSeconds(1) 16 | , TimeSpan.FromSeconds(2)); 17 | try 18 | { 19 | Sleep(TimeSpan.FromSeconds(6)); 20 | 21 | _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(4)); 22 | 23 | ReadLine(); 24 | } 25 | finally 26 | { 27 | _timer.Dispose(); 28 | } 29 | } 30 | 31 | static Timer _timer; 32 | 33 | static void TimerOperation(DateTime start) 34 | { 35 | TimeSpan elapsed = DateTime.Now - start; 36 | WriteLine($"{elapsed.Seconds} seconds from {start}. " + 37 | $"Timer thread pool thread id: {CurrentThread.ManagedThreadId}"); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /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 static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter3.Recipe7 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var bw = new BackgroundWorker(); 13 | bw.WorkerReportsProgress = true; 14 | bw.WorkerSupportsCancellation = true; 15 | 16 | bw.DoWork += Worker_DoWork; 17 | bw.ProgressChanged += Worker_ProgressChanged; 18 | bw.RunWorkerCompleted += Worker_Completed; 19 | 20 | bw.RunWorkerAsync(); 21 | 22 | WriteLine("Press C to cancel work"); 23 | do 24 | { 25 | if (ReadKey(true).KeyChar == 'C') 26 | { 27 | bw.CancelAsync(); 28 | } 29 | 30 | } 31 | while(bw.IsBusy); 32 | } 33 | 34 | static void Worker_DoWork(object sender, DoWorkEventArgs e) 35 | { 36 | WriteLine($"DoWork thread pool thread id: {CurrentThread.ManagedThreadId}"); 37 | var bw = (BackgroundWorker) sender; 38 | for (int i = 1; i <= 100; i++) 39 | { 40 | if (bw.CancellationPending) 41 | { 42 | e.Cancel = true; 43 | return; 44 | } 45 | if (i%10 == 0) 46 | { 47 | bw.ReportProgress(i); 48 | } 49 | 50 | Sleep(TimeSpan.FromSeconds(0.1)); 51 | } 52 | 53 | e.Result = 42; 54 | } 55 | 56 | static void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 57 | { 58 | WriteLine($"{e.ProgressPercentage}% completed. " + 59 | $"Progress thread pool thread id: {CurrentThread.ManagedThreadId}"); 60 | } 61 | 62 | static void Worker_Completed(object sender, RunWorkerCompletedEventArgs e) 63 | { 64 | WriteLine($"Completed thread pool thread id: {CurrentThread.ManagedThreadId}"); 65 | if (e.Error != null) 66 | { 67 | WriteLine($"Exception {e.Error.Message} has occured."); 68 | } 69 | else if (e.Cancelled) 70 | { 71 | WriteLine($"Operation has been canceled."); 72 | } 73 | else 74 | { 75 | WriteLine($"The answer is: {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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter4.Recipe1 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var t1 = new Task(() => TaskMethod("Task 1")); 13 | var t2 = new Task(() => TaskMethod("Task 2")); 14 | t2.Start(); 15 | t1.Start(); 16 | Task.Run(() => TaskMethod("Task 3")); 17 | Task.Factory.StartNew(() => TaskMethod("Task 4")); 18 | Task.Factory.StartNew(() => TaskMethod("Task 5"), TaskCreationOptions.LongRunning); 19 | Sleep(TimeSpan.FromSeconds(1)); 20 | } 21 | 22 | static void TaskMethod(string name) 23 | { 24 | WriteLine($"Task {name} is running on a thread id " + 25 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 26 | $"{CurrentThread.IsThreadPoolThread}"); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter4.Recipe2 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | TaskMethod("Main Thread Task"); 13 | Task task = CreateTask("Task 1"); 14 | task.Start(); 15 | int result = task.Result; 16 | WriteLine($"Result is: {result}"); 17 | 18 | task = CreateTask("Task 2"); 19 | task.RunSynchronously(); 20 | result = task.Result; 21 | WriteLine($"Result is: {result}"); 22 | 23 | task = CreateTask("Task 3"); 24 | WriteLine(task.Status); 25 | task.Start(); 26 | 27 | while (!task.IsCompleted) 28 | { 29 | WriteLine(task.Status); 30 | Sleep(TimeSpan.FromSeconds(0.5)); 31 | } 32 | 33 | WriteLine(task.Status); 34 | result = task.Result; 35 | WriteLine($"Result is: {result}"); 36 | } 37 | 38 | static Task CreateTask(string name) 39 | { 40 | return new Task(() => TaskMethod(name)); 41 | } 42 | 43 | static int TaskMethod(string name) 44 | { 45 | WriteLine($"Task {name} is running on a thread id " + 46 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 47 | $"{CurrentThread.IsThreadPoolThread}"); 48 | Sleep(TimeSpan.FromSeconds(2)); 49 | return 42; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /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/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/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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Threading.Tasks; 4 | using static System.Console; 5 | using static System.Threading.Thread; 6 | 7 | namespace Chapter4.Recipe5 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var tcs = new TaskCompletionSource(); 14 | 15 | var worker = new BackgroundWorker(); 16 | worker.DoWork += (sender, eventArgs) => 17 | { 18 | eventArgs.Result = TaskMethod("Background worker", 5); 19 | }; 20 | 21 | worker.RunWorkerCompleted += (sender, eventArgs) => 22 | { 23 | if (eventArgs.Error != null) 24 | { 25 | tcs.SetException(eventArgs.Error); 26 | } 27 | else if (eventArgs.Cancelled) 28 | { 29 | tcs.SetCanceled(); 30 | } 31 | else 32 | { 33 | tcs.SetResult((int)eventArgs.Result); 34 | } 35 | }; 36 | 37 | worker.RunWorkerAsync(); 38 | 39 | int result = tcs.Task.Result; 40 | 41 | WriteLine($"Result is: {result}"); 42 | } 43 | 44 | static int TaskMethod(string name, int seconds) 45 | { 46 | WriteLine( 47 | $"Task {name} is running on a thread id " + 48 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 49 | $"{CurrentThread.IsThreadPoolThread}"); 50 | 51 | Sleep(TimeSpan.FromSeconds(seconds)); 52 | return 42 * seconds; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using static System.Console; 5 | using static System.Threading.Thread; 6 | 7 | namespace Chapter4.Recipe6 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var cts = new CancellationTokenSource(); 14 | var longTask = new Task(() => TaskMethod("Task 1", 10, cts.Token), cts.Token); 15 | WriteLine(longTask.Status); 16 | cts.Cancel(); 17 | WriteLine(longTask.Status); 18 | WriteLine("First task has been cancelled before execution"); 19 | 20 | cts = new CancellationTokenSource(); 21 | longTask = new Task(() => TaskMethod("Task 2", 10, cts.Token), cts.Token); 22 | longTask.Start(); 23 | for (int i = 0; i < 5; i++ ) 24 | { 25 | Sleep(TimeSpan.FromSeconds(0.5)); 26 | WriteLine(longTask.Status); 27 | } 28 | cts.Cancel(); 29 | for (int i = 0; i < 5; i++) 30 | { 31 | Sleep(TimeSpan.FromSeconds(0.5)); 32 | WriteLine(longTask.Status); 33 | } 34 | 35 | WriteLine($"A task has been completed with result {longTask.Result}."); 36 | } 37 | 38 | static int TaskMethod(string name, int seconds, CancellationToken token) 39 | { 40 | WriteLine( 41 | $"Task {name} is running on a thread id " + 42 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 43 | $"{CurrentThread.IsThreadPoolThread}"); 44 | 45 | for (int i = 0; i < seconds; i ++) 46 | { 47 | Sleep(TimeSpan.FromSeconds(1)); 48 | if (token.IsCancellationRequested) return -1; 49 | } 50 | return 42*seconds; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using static System.Console; 4 | using static System.Threading.Thread; 5 | 6 | namespace Chapter4.Recipe7 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | Task task; 13 | try 14 | { 15 | task = Task.Run(() => TaskMethod("Task 1", 2)); 16 | int result = task.Result; 17 | WriteLine($"Result: {result}"); 18 | } 19 | catch (Exception ex) 20 | { 21 | WriteLine($"Exception caught: {ex}"); 22 | } 23 | WriteLine("----------------------------------------------"); 24 | WriteLine(); 25 | 26 | try 27 | { 28 | task = Task.Run(() => TaskMethod("Task 2", 2)); 29 | int result = task.GetAwaiter().GetResult(); 30 | WriteLine($"Result: {result}"); 31 | } 32 | catch (Exception ex) 33 | { 34 | WriteLine($"Exception caught: {ex}"); 35 | } 36 | WriteLine("----------------------------------------------"); 37 | WriteLine(); 38 | 39 | var t1 = new Task(() => TaskMethod("Task 3", 3)); 40 | var t2 = new Task(() => TaskMethod("Task 4", 2)); 41 | var complexTask = Task.WhenAll(t1, t2); 42 | var exceptionHandler = complexTask.ContinueWith(t => 43 | WriteLine($"Exception caught: {t.Exception}"), 44 | TaskContinuationOptions.OnlyOnFaulted 45 | ); 46 | t1.Start(); 47 | t2.Start(); 48 | 49 | Sleep(TimeSpan.FromSeconds(5)); 50 | } 51 | 52 | static int TaskMethod(string name, int seconds) 53 | { 54 | WriteLine( 55 | $"Task {name} is running on a thread id " + 56 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 57 | $"{CurrentThread.IsThreadPoolThread}"); 58 | 59 | Sleep(TimeSpan.FromSeconds(seconds)); 60 | throw new Exception("Boom!"); 61 | return 42 * seconds; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /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/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | using static System.Console; 5 | using static System.Threading.Thread; 6 | 7 | namespace Chapter4.Recipe8 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var firstTask = new Task(() => TaskMethod("First Task", 3)); 14 | var secondTask = new Task(() => TaskMethod("Second Task", 2)); 15 | var whenAllTask = Task.WhenAll(firstTask, secondTask); 16 | 17 | whenAllTask.ContinueWith(t => 18 | WriteLine($"The first answer is {t.Result[0]}, the second is {t.Result[1]}"), 19 | TaskContinuationOptions.OnlyOnRanToCompletion); 20 | 21 | firstTask.Start(); 22 | secondTask.Start(); 23 | 24 | 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($"Task {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 | WriteLine($"A task has been completed with result {completedTask.Result}."); 40 | } 41 | 42 | Sleep(TimeSpan.FromSeconds(1)); 43 | } 44 | 45 | static int TaskMethod(string name, int seconds) 46 | { 47 | WriteLine( 48 | $"Task {name} is running on a thread id " + 49 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " + 50 | $"{CurrentThread.IsThreadPoolThread}"); 51 | 52 | Sleep(TimeSpan.FromSeconds(seconds)); 53 | return 42 * seconds; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /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.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 |