├── .gitattributes
├── .gitignore
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── BookSamples_VS2013ExpressWindows.sln
├── BookSamples_VS2013ExpressWindowsDesktop.sln
├── BookSamples_VisualStudio2013.sln
├── Chapter1
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe10
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe10.csproj
├── Recipe11
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe11.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe9.csproj
├── Chapter10
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe3.csproj
│ └── packages.config
└── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Chapter11
├── Recipe1
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── Assets
│ │ ├── Logo.png
│ │ ├── SmallLogo.png
│ │ ├── SplashScreen.png
│ │ └── StoreLogo.png
│ ├── Common
│ │ └── StandardStyles.xaml
│ ├── MainPage.xaml
│ ├── MainPage.xaml.cs
│ ├── Package.appxmanifest
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
└── Recipe3
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── Assets
│ ├── Logo.png
│ ├── SmallLogo-Badge.png
│ ├── SmallLogo.png
│ ├── SplashScreen.png
│ └── StoreLogo.png
│ ├── Common
│ └── StandardStyles.xaml
│ ├── MainPage.xaml
│ ├── MainPage.xaml.cs
│ ├── Package.appxmanifest
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Chapter2
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe9.csproj
├── Chapter3
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
└── Recipe7
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Chapter4
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── App.config
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── MainWindow.xaml
│ ├── MainWindow.xaml.cs
│ ├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
│ └── Recipe9.csproj
├── Chapter5
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Recipe9.csproj
│ └── packages.config
├── Chapter6
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
└── Recipe5
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Chapter7
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
└── Recipe6
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Chapter8
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe1.csproj
│ └── packages.config
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe2.csproj
│ └── packages.config
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe3.csproj
│ └── packages.config
├── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe4.csproj
│ └── packages.config
├── Recipe5
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe5.csproj
│ └── packages.config
└── Recipe6
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Recipe6.csproj
│ └── packages.config
├── Chapter9
├── Recipe1
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
└── Recipe4
│ ├── App.config
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── LICENSE
├── README.md
└── test
└── test.sln
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/Chapter1/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe1
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Thread t = new Thread(PrintNumbers);
11 | t.Start();
12 | PrintNumbers();
13 | }
14 |
15 | static void PrintNumbers()
16 | {
17 | Console.WriteLine("Starting...");
18 | for (int i = 1; i < 10; i++)
19 | {
20 | Console.WriteLine(i);
21 | }
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/Chapter1/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("0ceeb3db-93f2-4154-9013-d777321f3f17")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe10/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe10
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | object lock1 = new object();
11 | object lock2 = new object();
12 |
13 | new Thread(() => LockTooMuch(lock1, lock2)).Start();
14 |
15 | lock (lock2)
16 | {
17 | Thread.Sleep(1000);
18 | Console.WriteLine("Monitor.TryEnter allows not to get stuck, returning false after a specified timeout is elapsed");
19 | if (Monitor.TryEnter(lock1, TimeSpan.FromSeconds(5)))
20 | {
21 | Console.WriteLine("Acquired a protected resource succesfully");
22 | }
23 | else
24 | {
25 | Console.WriteLine("Timeout acquiring a resource!");
26 | }
27 | }
28 |
29 | new Thread(() => LockTooMuch(lock1, lock2)).Start();
30 |
31 | Console.WriteLine("----------------------------------");
32 | lock (lock2)
33 | {
34 | Console.WriteLine("This will be a deadlock!");
35 | Thread.Sleep(1000);
36 | lock (lock1)
37 | {
38 | Console.WriteLine("Acquired a protected resource succesfully");
39 | }
40 | }
41 | }
42 |
43 | static void LockTooMuch(object lock1, object lock2)
44 | {
45 | lock (lock1)
46 | {
47 | Thread.Sleep(1000);
48 | lock (lock2);
49 | }
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter1/Recipe10/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe10")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe10")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("27746d59-c51e-4c29-a543-f9b3255fbc9d")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe11/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe11
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var t = new Thread(FaultyThread);
11 | t.Start();
12 | t.Join();
13 |
14 | try
15 | {
16 | t = new Thread(BadFaultyThread);
17 | t.Start();
18 | }
19 | catch (Exception ex)
20 | {
21 | Console.WriteLine("We won't get here!");
22 | }
23 | }
24 |
25 | static void BadFaultyThread()
26 | {
27 | Console.WriteLine("Starting a faulty thread...");
28 | Thread.Sleep(TimeSpan.FromSeconds(2));
29 | throw new Exception("Boom!");
30 | }
31 |
32 | static void FaultyThread()
33 | {
34 | try
35 | {
36 | Console.WriteLine("Starting a faulty thread...");
37 | Thread.Sleep(TimeSpan.FromSeconds(1));
38 | throw new Exception("Boom!");
39 | }
40 | catch (Exception ex)
41 | {
42 | Console.WriteLine("Exception handled: {0}", ex.Message);
43 | }
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Chapter1/Recipe11/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe14")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe14")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("fbf98b90-1596-4fca-bafb-44ce73b4718a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe2
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Thread t = new Thread(PrintNumbersWithDelay);
11 | t.Start();
12 | PrintNumbers();
13 | }
14 |
15 | static void PrintNumbers()
16 | {
17 | Console.WriteLine("Starting...");
18 | for (int i = 1; i < 10; i++)
19 | {
20 | Console.WriteLine(i);
21 | }
22 | }
23 |
24 | static void PrintNumbersWithDelay()
25 | {
26 | Console.WriteLine("Starting...");
27 | for (int i = 1; i < 10; i++)
28 | {
29 | Thread.Sleep(TimeSpan.FromSeconds(2));
30 | Console.WriteLine(i);
31 | }
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/Chapter1/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("28b0ed05-0050-4d9f-b814-26dd1d236523")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe3
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Console.WriteLine("Starting program...");
11 | Thread t = new Thread(PrintNumbersWithDelay);
12 | t.Start();
13 | t.Join();
14 | Console.WriteLine("Thread completed");
15 | }
16 |
17 | static void PrintNumbersWithDelay()
18 | {
19 | Console.WriteLine("Starting...");
20 | for (int i = 1; i < 10; i++)
21 | {
22 | Thread.Sleep(TimeSpan.FromSeconds(2));
23 | Console.WriteLine(i);
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter1/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("56583f3d-119b-4aca-840b-bf2ee53db6fd")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe4
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Console.WriteLine("Starting program...");
11 | Thread t = new Thread(PrintNumbersWithDelay);
12 | t.Start();
13 | Thread.Sleep(TimeSpan.FromSeconds(6));
14 | t.Abort();
15 | Console.WriteLine("A thread has been aborted");
16 | }
17 |
18 | static void PrintNumbersWithDelay()
19 | {
20 | Console.WriteLine("Starting...");
21 | for (int i = 1; i < 10; i++)
22 | {
23 | Thread.Sleep(TimeSpan.FromSeconds(2));
24 | Console.WriteLine(i);
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter1/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("78b7a1e7-835b-4aec-a556-94d6b76e7080")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe5
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Console.WriteLine("Starting program...");
11 | Thread t = new Thread(PrintNumbersWithStatus);
12 | Thread t2 = new Thread(DoNothing);
13 | Console.WriteLine(t.ThreadState.ToString());
14 | t2.Start();
15 | t.Start();
16 | for (int i = 1; i < 30; i++)
17 | {
18 | Console.WriteLine(t.ThreadState.ToString());
19 | }
20 | Thread.Sleep(TimeSpan.FromSeconds(6));
21 | t.Abort();
22 | Console.WriteLine("A thread has been aborted");
23 | Console.WriteLine(t.ThreadState.ToString());
24 | Console.WriteLine(t2.ThreadState.ToString());
25 | }
26 |
27 | static void DoNothing()
28 | {
29 | Thread.Sleep(TimeSpan.FromSeconds(2));
30 | }
31 |
32 | static void PrintNumbersWithStatus()
33 | {
34 | Console.WriteLine("Starting...");
35 | Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());
36 | for (int i = 1; i < 10; i++)
37 | {
38 | Thread.Sleep(TimeSpan.FromSeconds(2));
39 | Console.WriteLine(i);
40 | }
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter1/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("3004638c-a1c6-4c9a-ae23-8b1c080d5682")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading;
4 |
5 | namespace Chapter1.Recipe6
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Console.WriteLine("Current thread priority: {0}", Thread.CurrentThread.Priority);
12 | Console.WriteLine("Running on all cores available");
13 | RunThreads();
14 | Thread.Sleep(TimeSpan.FromSeconds(2));
15 | Console.WriteLine("Running on a single core");
16 | Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
17 | RunThreads();
18 | }
19 |
20 | static void RunThreads()
21 | {
22 | var sample = new ThreadSample();
23 |
24 | var threadOne = new Thread(sample.CountNumbers);
25 | threadOne.Name = "ThreadOne";
26 | var threadTwo = new Thread(sample.CountNumbers);
27 | threadTwo.Name = "ThreadTwo";
28 |
29 | threadOne.Priority = ThreadPriority.Highest;
30 | threadTwo.Priority = ThreadPriority.Lowest;
31 | threadOne.Start();
32 | threadTwo.Start();
33 |
34 | Thread.Sleep(TimeSpan.FromSeconds(2));
35 | sample.Stop();
36 | }
37 |
38 | class ThreadSample
39 | {
40 | private bool _isStopped = false;
41 |
42 | public void Stop()
43 | {
44 | _isStopped = true;
45 | }
46 |
47 | public void CountNumbers()
48 | {
49 | long counter = 0;
50 |
51 | while (!_isStopped)
52 | {
53 | counter++;
54 | }
55 |
56 | Console.WriteLine("{0} with {1,11} priority " +
57 | "has a count = {2,13}", Thread.CurrentThread.Name,
58 | Thread.CurrentThread.Priority,
59 | counter.ToString("N0"));
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/Chapter1/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("2b30c7ae-5ce3-49bb-9cdf-d2516d2290be")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe7
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var sampleForeground = new ThreadSample(10);
11 | var sampleBackground = new ThreadSample(20);
12 |
13 | var threadOne = new Thread(sampleForeground.CountNumbers);
14 | threadOne.Name = "ForegroundThread";
15 | var threadTwo = new Thread(sampleBackground.CountNumbers);
16 | threadTwo.Name = "BackgroundThread";
17 | threadTwo.IsBackground = true;
18 |
19 | threadOne.Start();
20 | threadTwo.Start();
21 | }
22 |
23 | class ThreadSample
24 | {
25 | private readonly int _iterations;
26 |
27 | public ThreadSample(int iterations)
28 | {
29 | _iterations = iterations;
30 | }
31 | public void CountNumbers()
32 | {
33 | for (int i = 0; i < _iterations; i++)
34 | {
35 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
36 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
37 | }
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter1/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("49a9121c-996a-4eb5-a0e8-e86475e0dd16")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter1.Recipe8
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var sample = new ThreadSample(10);
11 |
12 | var threadOne = new Thread(sample.CountNumbers);
13 | threadOne.Name = "ThreadOne";
14 | threadOne.Start();
15 | threadOne.Join();
16 |
17 | Console.WriteLine("--------------------------");
18 |
19 | var threadTwo = new Thread(Count);
20 | threadTwo.Name = "ThreadTwo";
21 | threadTwo.Start(8);
22 | threadTwo.Join();
23 |
24 | Console.WriteLine("--------------------------");
25 |
26 | var threadThree = new Thread(() => CountNumbers(12));
27 | threadThree.Name = "ThreadThree";
28 | threadThree.Start();
29 | threadThree.Join();
30 | Console.WriteLine("--------------------------");
31 |
32 | int i = 10;
33 | var threadFour = new Thread(() => PrintNumber(i));
34 | i = 20;
35 | var threadFive = new Thread(() => PrintNumber(i));
36 | threadFour.Start();
37 | threadFive.Start();
38 | }
39 |
40 | static void Count(object iterations)
41 | {
42 | CountNumbers((int)iterations);
43 | }
44 |
45 | static void CountNumbers(int iterations)
46 | {
47 | for (int i = 1; i <= iterations; i++)
48 | {
49 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
50 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
51 | }
52 | }
53 |
54 | static void PrintNumber(int number)
55 | {
56 | Console.WriteLine(number);
57 | }
58 |
59 | class ThreadSample
60 | {
61 | private readonly int _iterations;
62 |
63 | public ThreadSample(int iterations)
64 | {
65 | _iterations = iterations;
66 | }
67 | public void CountNumbers()
68 | {
69 | for (int i = 1; i <= _iterations; i++)
70 | {
71 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
72 | Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
73 | }
74 | }
75 | }
76 | }
77 |
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/Chapter1/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("2cbac03d-c6da-4d55-98cd-9bdfa2e11564")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe9/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe9")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe9")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("a07629bb-ca3f-42d6-8ea6-c99bf31637a8")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter10/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("52b0193d-762f-4d4f-a8fe-7c86237d1270")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter10/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("be99ea07-302f-490c-a775-f1706d4d10c0")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter10/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f399b1fb-7f3f-4bbc-807a-9ab99f4f1d1f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe3/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter10/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter10/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("63702efc-ff1e-43cb-97d4-6ae930b2feb7")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/Logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/Logo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/SmallLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/SmallLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/SplashScreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/SplashScreen.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/StoreLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe1/Assets/StoreLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/MainPage.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Package.appxmanifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Chapter11.Recipe1
6 | Eugene
7 | Assets\StoreLogo.png
8 |
9 |
10 | 6.3.0
11 | 6.3.0
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Version information for an assembly consists of the following four values:
18 | //
19 | // Major Version
20 | // Minor Version
21 | // Build Number
22 | // Revision
23 | //
24 | // You can specify all the values or you can default the Build and Revision Numbers
25 | // by using the '*' as shown below:
26 | // [assembly: AssemblyVersion("1.0.*")]
27 | [assembly: AssemblyVersion("1.0.0.0")]
28 | [assembly: AssemblyFileVersion("1.0.0.0")]
29 | [assembly: ComVisible(false)]
--------------------------------------------------------------------------------
/Chapter11/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter11/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("e6053ad8-4fb1-42ff-8b68-55932e053dfc")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/Logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/Logo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/SmallLogo-Badge.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SmallLogo-Badge.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/SmallLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SmallLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/SplashScreen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/SplashScreen.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/StoreLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eugeneagafonov/multithreadingcookbook/5b75974ac5e62b535200cd511f047be153718778/Chapter11/Recipe3/Assets/StoreLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/MainPage.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Package.appxmanifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Recipe3
6 | Eugene
7 | Assets\StoreLogo.png
8 |
9 |
10 | 6.3.0
11 | 6.3.0
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Version information for an assembly consists of the following four values:
18 | //
19 | // Major Version
20 | // Minor Version
21 | // Build Number
22 | // Revision
23 | //
24 | // You can specify all the values or you can default the Build and Revision Numbers
25 | // by using the '*' as shown below:
26 | // [assembly: AssemblyVersion("1.0.*")]
27 | [assembly: AssemblyVersion("1.0.0.0")]
28 | [assembly: AssemblyFileVersion("1.0.0.0")]
29 | [assembly: ComVisible(false)]
--------------------------------------------------------------------------------
/Chapter2/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe1
5 | {
6 | internal class Program
7 | {
8 | private static void Main(string[] args)
9 | {
10 | Console.WriteLine("Incorrect counter");
11 |
12 | var c = new Counter();
13 |
14 | var t1 = new Thread(() => TestCounter(c));
15 | var t2 = new Thread(() => TestCounter(c));
16 | var t3 = new Thread(() => TestCounter(c));
17 | t1.Start();
18 | t2.Start();
19 | t3.Start();
20 | t1.Join();
21 | t2.Join();
22 | t3.Join();
23 |
24 | Console.WriteLine("Total count: {0}", c.Count);
25 | Console.WriteLine("--------------------------");
26 |
27 | Console.WriteLine("Correct counter");
28 |
29 | var c1 = new CounterNoLock();
30 |
31 | t1 = new Thread(() => TestCounter(c1));
32 | t2 = new Thread(() => TestCounter(c1));
33 | t3 = new Thread(() => TestCounter(c1));
34 | t1.Start();
35 | t2.Start();
36 | t3.Start();
37 | t1.Join();
38 | t2.Join();
39 | t3.Join();
40 |
41 | Console.WriteLine("Total count: {0}", c1.Count);
42 | }
43 |
44 | static void TestCounter(CounterBase c)
45 | {
46 | for (int i = 0; i < 100000; i++)
47 | {
48 | c.Increment();
49 | c.Decrement();
50 | }
51 | }
52 |
53 | class Counter : CounterBase
54 | {
55 | private int _count;
56 |
57 | public int Count { get { return _count; } }
58 |
59 | public override void Increment()
60 | {
61 | _count++;
62 | }
63 |
64 | public override void Decrement()
65 | {
66 | _count--;
67 | }
68 | }
69 |
70 | class CounterNoLock : CounterBase
71 | {
72 | private int _count;
73 |
74 | public int Count { get { return _count; } }
75 |
76 | public override void Increment()
77 | {
78 | Interlocked.Increment(ref _count);
79 | }
80 |
81 | public override void Decrement()
82 | {
83 | Interlocked.Decrement(ref _count);
84 | }
85 | }
86 |
87 | abstract class CounterBase
88 | {
89 | public abstract void Increment();
90 |
91 | public abstract void Decrement();
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/Chapter2/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("944a1eb5-90f6-4365-98a0-b45118f56175")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe2
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | const string MutexName = "CSharpThreadingCookbook";
11 |
12 | using (var m = new Mutex(false, MutexName))
13 | {
14 | if (!m.WaitOne(TimeSpan.FromSeconds(5), false))
15 | {
16 | Console.WriteLine("Second instance is running!");
17 | }
18 | else
19 | {
20 | Console.WriteLine("Running!");
21 | Console.ReadLine();
22 | m.ReleaseMutex();
23 | }
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter2/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c63b4870-e7e6-42b1-aba2-f0f6b24fa94f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe3
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | for (int i = 1; i <= 6; i++)
11 | {
12 | string threadName = "Thread " + i;
13 | int secondsToWait = 2 + 2 * i;
14 | var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
15 | t.Start();
16 | }
17 | }
18 |
19 | static SemaphoreSlim _semaphore = new SemaphoreSlim(4);
20 |
21 | static void AccessDatabase(string name, int seconds)
22 | {
23 | Console.WriteLine("{0} waits to access a database", name);
24 | _semaphore.Wait();
25 | Console.WriteLine("{0} was granted an access to a database", name);
26 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
27 | Console.WriteLine("{0} is completed", name);
28 | _semaphore.Release();
29 |
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Chapter2/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7fcc52db-8509-4f1a-9d02-7921a430505d")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe4
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var t = new Thread(() => Process(10));
11 | t.Start();
12 |
13 | Console.WriteLine("Waiting for another thread to complete work");
14 | _workerEvent.WaitOne();
15 | Console.WriteLine("First operation is completed!");
16 | Console.WriteLine("Performing an operation on a main thread");
17 | Thread.Sleep(TimeSpan.FromSeconds(5));
18 | _mainEvent.Set();
19 | Console.WriteLine("Now running the second operation on a second thread");
20 | _workerEvent.WaitOne();
21 | Console.WriteLine("Second operation is completed!");
22 | }
23 |
24 | private static AutoResetEvent _workerEvent = new AutoResetEvent(false);
25 | private static AutoResetEvent _mainEvent = new AutoResetEvent(false);
26 |
27 | static void Process(int seconds)
28 | {
29 | Console.WriteLine("Starting a long running work...");
30 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
31 | Console.WriteLine("Work is done!");
32 | _workerEvent.Set();
33 | Console.WriteLine("Waiting for a main thread to complete its work");
34 | _mainEvent.WaitOne();
35 | Console.WriteLine("Starting second operation...");
36 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
37 | Console.WriteLine("Work is done!");
38 | _workerEvent.Set();
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter2/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8bad7962-39eb-4047-a3b7-024fda286eec")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe5
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var t1 = new Thread(() => TravelThroughGates("Thread 1", 5));
11 | var t2 = new Thread(() => TravelThroughGates("Thread 2", 6));
12 | var t3 = new Thread(() => TravelThroughGates("Thread 3", 12));
13 | t1.Start();
14 | t2.Start();
15 | t3.Start();
16 | Thread.Sleep(TimeSpan.FromSeconds(6));
17 | Console.WriteLine("The gates are now open!");
18 | _mainEvent.Set();
19 | Thread.Sleep(TimeSpan.FromSeconds(2));
20 | _mainEvent.Reset();
21 | Console.WriteLine("The gates have been closed!");
22 | Thread.Sleep(TimeSpan.FromSeconds(10));
23 | Console.WriteLine("The gates are now open for the second time!");
24 | _mainEvent.Set();
25 | Thread.Sleep(TimeSpan.FromSeconds(2));
26 | Console.WriteLine("The gates have been closed!");
27 | _mainEvent.Reset();
28 | }
29 |
30 | static void TravelThroughGates(string threadName, int seconds)
31 | {
32 | Console.WriteLine("{0} falls to sleep", threadName);
33 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
34 | Console.WriteLine("{0} waits for the gates to open!", threadName);
35 | _mainEvent.Wait();
36 | Console.WriteLine("{0} enters the gates!", threadName);
37 | }
38 |
39 | static ManualResetEventSlim _mainEvent = new ManualResetEventSlim(false);
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Chapter2/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("793034d9-1c36-4858-a4cc-7182286e0ff8")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe6
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Console.WriteLine("Starting two operations");
11 | var t1 = new Thread(() => PerformOperation("Operation 1 is completed", 4));
12 | var t2 = new Thread(() => PerformOperation("Operation 2 is completed", 8));
13 | t1.Start();
14 | t2.Start();
15 | _countdown.Wait();
16 | Console.WriteLine("Both operations have been completed.");
17 | _countdown.Dispose();
18 | }
19 |
20 | static CountdownEvent _countdown = new CountdownEvent(2);
21 |
22 | static void PerformOperation(string message, int seconds)
23 | {
24 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
25 | Console.WriteLine(message);
26 | _countdown.Signal();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter2/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7732c65d-fdd1-4322-ad33-2ab587390a3e")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe7
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var t1 = new Thread(() => PlayMusic("the guitarist", "play an amazing solo", 5));
11 | var t2 = new Thread(() => PlayMusic("the singer", "sing his song", 2));
12 |
13 | t1.Start();
14 | t2.Start();
15 | }
16 |
17 | static Barrier _barrier = new Barrier(2,
18 | b => Console.WriteLine("End of phase {0}", b.CurrentPhaseNumber + 1));
19 |
20 | static void PlayMusic(string name, string message, int seconds)
21 | {
22 | for (int i = 1; i < 3; i++)
23 | {
24 | Console.WriteLine("----------------------------------------------");
25 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
26 | Console.WriteLine("{0} starts to {1}", name, message);
27 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
28 | Console.WriteLine("{0} finishes to {1}", name, message);
29 | _barrier.SignalAndWait();
30 | }
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter2/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c8637971-a29b-4354-8a16-c812cd0af604")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading;
4 |
5 | namespace Chapter2.Recipe8
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | new Thread(Read){ IsBackground = true }.Start();
12 | new Thread(Read){ IsBackground = true }.Start();
13 | new Thread(Read){ IsBackground = true }.Start();
14 |
15 | new Thread(() => Write("Thread 1")){ IsBackground = true }.Start();
16 | new Thread(() => Write("Thread 2")){ IsBackground = true }.Start();
17 |
18 | Thread.Sleep(TimeSpan.FromSeconds(30));
19 | }
20 |
21 | static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim();
22 | static Dictionary _items = new Dictionary();
23 |
24 | static void Read()
25 | {
26 | Console.WriteLine("Reading contents of a dictionary");
27 | while (true)
28 | {
29 | try
30 | {
31 | _rw.EnterReadLock();
32 | foreach (var key in _items.Keys)
33 | {
34 | Thread.Sleep(TimeSpan.FromSeconds(0.1));
35 | }
36 | }
37 | finally
38 | {
39 | _rw.ExitReadLock();
40 | }
41 | }
42 | }
43 |
44 | static void Write(string threadName)
45 | {
46 | while (true)
47 | {
48 | try
49 | {
50 | int newKey = new Random().Next(250);
51 | _rw.EnterUpgradeableReadLock();
52 | if (!_items.ContainsKey(newKey))
53 | {
54 | try
55 | {
56 | _rw.EnterWriteLock();
57 | _items[newKey] = 1;
58 | Console.WriteLine("New key {0} is added to a dictionary by a {1}", newKey, threadName);
59 | }
60 | finally
61 | {
62 | _rw.ExitWriteLock();
63 | }
64 | }
65 | Thread.Sleep(TimeSpan.FromSeconds(0.1));
66 | }
67 | finally
68 | {
69 | _rw.ExitUpgradeableReadLock();
70 | }
71 | }
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Chapter2/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4489f834-2752-456f-8baf-103d84cf6841")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe9/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter2.Recipe9
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | var t1 = new Thread(UserModeWait);
11 | var t2 = new Thread(HybridSpinWait);
12 |
13 | Console.WriteLine("Running user mode waiting");
14 | t1.Start();
15 | Thread.Sleep(20);
16 | _isCompleted = true;
17 | Thread.Sleep(TimeSpan.FromSeconds(1));
18 | _isCompleted = false;
19 | Console.WriteLine("Running hybrid SpinWait construct waiting");
20 | t2.Start();
21 | Thread.Sleep(5);
22 | _isCompleted = true;
23 | }
24 |
25 | static volatile bool _isCompleted = false;
26 |
27 | static void UserModeWait()
28 | {
29 | while (!_isCompleted)
30 | {
31 | Console.Write(".");
32 | }
33 | Console.WriteLine();
34 | Console.WriteLine("Waiting is complete");
35 | }
36 |
37 | static void HybridSpinWait()
38 | {
39 | var w = new SpinWait();
40 | while (!_isCompleted)
41 | {
42 | w.SpinOnce();
43 | Console.WriteLine(w.NextSpinWillYield);
44 | }
45 | Console.WriteLine("Waiting is complete");
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/Chapter2/Recipe9/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe9")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe9")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7bdbe352-3307-41db-b076-3a7d4727a1d5")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter3.Recipe1
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | int threadId = 0;
11 |
12 | RunOnThreadPool poolDelegate = Test;
13 |
14 | var t = new Thread(() => Test(out threadId));
15 | t.Start();
16 | t.Join();
17 |
18 | Console.WriteLine("Thread id: {0}", threadId);
19 |
20 | IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
21 | r.AsyncWaitHandle.WaitOne();
22 |
23 | string result = poolDelegate.EndInvoke(out threadId, r);
24 |
25 | Console.WriteLine("Thread pool worker thread id: {0}", threadId);
26 | Console.WriteLine(result);
27 |
28 | Thread.Sleep(TimeSpan.FromSeconds(2));
29 | }
30 |
31 | private delegate string RunOnThreadPool(out int threadId);
32 |
33 | private static void Callback(IAsyncResult ar)
34 | {
35 | Console.WriteLine("Starting a callback...");
36 | Console.WriteLine("State passed to a callbak: {0}", ar.AsyncState);
37 | Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
38 | Console.WriteLine("Thread pool worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
39 | }
40 |
41 |
42 | private static string Test(out int threadId)
43 | {
44 | Console.WriteLine("Starting...");
45 | Console.WriteLine("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
46 | Thread.Sleep(TimeSpan.FromSeconds(2));
47 | threadId = Thread.CurrentThread.ManagedThreadId;
48 | return string.Format("Thread pool worker thread id was: {0}", threadId);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Chapter3/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f6db0a1e-2c72-496a-83fa-f3d9e60a4038")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter3.Recipe2
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | const int x = 1;
11 | const int y = 2;
12 | const string lambdaState = "lambda state 2";
13 |
14 | ThreadPool.QueueUserWorkItem(AsyncOperation);
15 | Thread.Sleep(TimeSpan.FromSeconds(1));
16 |
17 | ThreadPool.QueueUserWorkItem(AsyncOperation, "async state");
18 | Thread.Sleep(TimeSpan.FromSeconds(1));
19 |
20 | ThreadPool.QueueUserWorkItem( state => {
21 | Console.WriteLine("Operation state: {0}", state);
22 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
23 | Thread.Sleep(TimeSpan.FromSeconds(2));
24 | }, "lambda state");
25 |
26 | ThreadPool.QueueUserWorkItem( _ =>
27 | {
28 | Console.WriteLine("Operation state: {0}, {1}", x+y, lambdaState);
29 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
30 | Thread.Sleep(TimeSpan.FromSeconds(2));
31 | }, "lambda state");
32 |
33 | Thread.Sleep(TimeSpan.FromSeconds(2));
34 | }
35 |
36 | private static void AsyncOperation(object state)
37 | {
38 | Console.WriteLine("Operation state: {0}", state ?? "(null)");
39 | Console.WriteLine("Worker thread id: {0}", Thread.CurrentThread.ManagedThreadId);
40 | Thread.Sleep(TimeSpan.FromSeconds(2));
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter3/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("98411a29-94b5-4fe5-84aa-4be05008e3ab")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading;
4 |
5 | namespace Chapter3.Recipe3
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | const int numberOfOperations = 500;
12 | var sw = new Stopwatch();
13 | sw.Start();
14 | UseThreads(numberOfOperations);
15 | sw.Stop();
16 | Console.WriteLine("Execution time using threads: {0}", sw.ElapsedMilliseconds);
17 |
18 | sw.Reset();
19 | sw.Start();
20 | UseThreadPool(numberOfOperations);
21 | sw.Stop();
22 | Console.WriteLine("Execution time using threads: {0}", sw.ElapsedMilliseconds);
23 | }
24 |
25 | static void UseThreads(int numberOfOperations)
26 | {
27 | using (var countdown = new CountdownEvent(numberOfOperations))
28 | {
29 | Console.WriteLine("Scheduling work by creating threads");
30 | for (int i = 0; i < numberOfOperations; i++)
31 | {
32 | var thread = new Thread(() => {
33 | Console.Write("{0},", Thread.CurrentThread.ManagedThreadId);
34 | Thread.Sleep(TimeSpan.FromSeconds(0.1));
35 | countdown.Signal();
36 | });
37 | thread.Start();
38 | }
39 | countdown.Wait();
40 | Console.WriteLine();
41 | }
42 | }
43 |
44 | static void UseThreadPool(int numberOfOperations)
45 | {
46 | using (var countdown = new CountdownEvent(numberOfOperations))
47 | {
48 | Console.WriteLine("Starting work on a threadpool");
49 | for (int i = 0; i < numberOfOperations; i++)
50 | {
51 | ThreadPool.QueueUserWorkItem( _ => {
52 | Console.Write("{0},", Thread.CurrentThread.ManagedThreadId);
53 | Thread.Sleep(TimeSpan.FromSeconds(0.1));
54 | countdown.Signal();
55 | });
56 | }
57 | countdown.Wait();
58 | Console.WriteLine();
59 | }
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/Chapter3/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("59d0805c-d2ea-451a-9e73-5ca0f55d3189")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("70cc3a8a-9ecb-4c51-ad54-516cbda89010")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter3.Recipe5
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | RunOperations(TimeSpan.FromSeconds(5));
11 | RunOperations(TimeSpan.FromSeconds(7));
12 | }
13 |
14 | static void RunOperations(TimeSpan workerOperationTimeout)
15 | {
16 | using (var evt = new ManualResetEvent(false))
17 | using (var cts = new CancellationTokenSource())
18 | {
19 | Console.WriteLine("Registering timeout operations...");
20 | var worker = ThreadPool.RegisterWaitForSingleObject(evt,
21 | (state, isTimedOut) => WorkerOperationWait(cts, isTimedOut), null, workerOperationTimeout, true);
22 |
23 | Console.WriteLine("Starting long running operation...");
24 |
25 | ThreadPool.QueueUserWorkItem(_ => WorkerOperation(cts.Token, evt));
26 |
27 | Thread.Sleep(workerOperationTimeout.Add(TimeSpan.FromSeconds(2)));
28 | worker.Unregister(evt);
29 | }
30 | }
31 |
32 | static void WorkerOperation(CancellationToken token, ManualResetEvent evt)
33 | {
34 | for(int i = 0; i < 6; i++)
35 | {
36 | if (token.IsCancellationRequested)
37 | {
38 | return;
39 | }
40 | Thread.Sleep(TimeSpan.FromSeconds(1));
41 | }
42 | evt.Set();
43 | }
44 |
45 | static void WorkerOperationWait(CancellationTokenSource cts, bool isTimedOut)
46 | {
47 | if (isTimedOut)
48 | {
49 | cts.Cancel();
50 | Console.WriteLine("Worker operation timed out and was canceled.");
51 | }
52 | else
53 | {
54 | Console.WriteLine("Worker operation succeded.");
55 | }
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/Chapter3/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c2191460-ac35-448f-9f04-0c8ca5753006")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 |
4 | namespace Chapter3.Recipe6
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | Console.WriteLine("Press 'Enter' to stop the timer...");
11 | DateTime start = DateTime.Now;
12 | _timer = new Timer(_ => TimerOperation(start), null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
13 |
14 | Thread.Sleep(TimeSpan.FromSeconds(6));
15 |
16 | _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(4));
17 |
18 | Console.ReadLine();
19 |
20 | _timer.Dispose();
21 | }
22 |
23 | static Timer _timer;
24 |
25 | static void TimerOperation(DateTime start)
26 | {
27 | TimeSpan elapsed = DateTime.Now - start;
28 | Console.WriteLine("{0} seconds from {1}. Timer thread pool thread id: {2}", elapsed.Seconds, start,
29 | Thread.CurrentThread.ManagedThreadId);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Chapter3/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7fb66af4-43a4-4632-8535-0fa47a07d3d1")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.Threading;
4 |
5 | namespace Chapter3.Recipe7
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var bw = new BackgroundWorker();
12 | bw.WorkerReportsProgress = true;
13 | bw.WorkerSupportsCancellation = true;
14 |
15 | bw.DoWork += Worker_DoWork;
16 | bw.ProgressChanged += Worker_ProgressChanged;
17 | bw.RunWorkerCompleted += Worker_Completed;
18 |
19 | bw.RunWorkerAsync();
20 |
21 | Console.WriteLine("Press C to cancel work");
22 | do
23 | {
24 | if (Console.ReadKey(true).KeyChar == 'C')
25 | {
26 | bw.CancelAsync();
27 | }
28 |
29 | }
30 | while(bw.IsBusy);
31 | }
32 |
33 | static void Worker_DoWork(object sender, DoWorkEventArgs e)
34 | {
35 | Console.WriteLine("DoWork thread pool thread id: {0}", Thread.CurrentThread.ManagedThreadId);
36 | var bw = (BackgroundWorker) sender;
37 | for (int i = 1; i <= 100; i++)
38 | {
39 |
40 | if (bw.CancellationPending)
41 | {
42 | e.Cancel = true;
43 | return;
44 | }
45 |
46 | if (i%10 == 0)
47 | {
48 | bw.ReportProgress(i);
49 | }
50 |
51 | Thread.Sleep(TimeSpan.FromSeconds(0.1));
52 | }
53 | e.Result = 42;
54 | }
55 |
56 | static void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
57 | {
58 | Console.WriteLine("{0}% completed. Progress thread pool thread id: {1}", e.ProgressPercentage,
59 | Thread.CurrentThread.ManagedThreadId);
60 | }
61 |
62 | static void Worker_Completed(object sender, RunWorkerCompletedEventArgs e)
63 | {
64 | Console.WriteLine("Completed thread pool thread id: {0}", Thread.CurrentThread.ManagedThreadId);
65 | if (e.Error != null)
66 | {
67 | Console.WriteLine("Exception {0} has occured.", e.Error.Message);
68 | }
69 | else if (e.Cancelled)
70 | {
71 | Console.WriteLine("Operation has been canceled.");
72 | }
73 | else
74 | {
75 | Console.WriteLine("The answer is: {0}", e.Result);
76 | }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/Chapter3/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("d8017f66-6f29-462c-8f8e-59148d9aa447")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter4.Recipe1
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | var t1 = new Task(() => TaskMethod("Task 1"));
12 | var t2 = new Task(() => TaskMethod("Task 2"));
13 | t2.Start();
14 | t1.Start();
15 | Task.Run(() => TaskMethod("Task 3"));
16 | Task.Factory.StartNew(() => TaskMethod("Task 4"));
17 | Task.Factory.StartNew(() => TaskMethod("Task 5"), TaskCreationOptions.LongRunning);
18 | Thread.Sleep(TimeSpan.FromSeconds(1));
19 | }
20 |
21 | static void TaskMethod(string name)
22 | {
23 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
24 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter4/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("540b87a9-d995-4f22-b1d8-e72ffd74a205")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter4.Recipe2
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | TaskMethod("Main Thread Task");
12 | Task task = CreateTask("Task 1");
13 | task.Start();
14 | int result = task.Result;
15 | Console.WriteLine("Result is: {0}", result);
16 |
17 | task = CreateTask("Task 2");
18 | task.RunSynchronously();
19 | result = task.Result;
20 | Console.WriteLine("Result is: {0}", result);
21 |
22 | task = CreateTask("Task 3");
23 | Console.WriteLine(task.Status);
24 | task.Start();
25 |
26 | while (!task.IsCompleted)
27 | {
28 | Console.WriteLine(task.Status);
29 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
30 | }
31 |
32 | Console.WriteLine(task.Status);
33 | result = task.Result;
34 | Console.WriteLine("Result is: {0}", result);
35 | }
36 |
37 | static Task CreateTask(string name)
38 | {
39 | return new Task(() => TaskMethod(name));
40 | }
41 |
42 | static int TaskMethod(string name)
43 | {
44 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
45 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
46 | Thread.Sleep(TimeSpan.FromSeconds(2));
47 | return 42;
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter4/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("db157e8a-39d7-45b8-9938-a9087f6d4a1a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ac88c94e-5c89-41c6-a816-090957c2ac87")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ea8dfbb6-94db-4274-bacb-3a4712993fcb")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter4.Recipe5
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var tcs = new TaskCompletionSource();
13 |
14 | var worker = new BackgroundWorker();
15 | worker.DoWork += (sender, eventArgs) =>
16 | {
17 | eventArgs.Result = TaskMethod("Background worker", 5);
18 | };
19 |
20 | worker.RunWorkerCompleted += (sender, eventArgs) =>
21 | {
22 | if (eventArgs.Error != null)
23 | {
24 | tcs.SetException(eventArgs.Error);
25 | }
26 | else if (eventArgs.Cancelled)
27 | {
28 | tcs.SetCanceled();
29 | }
30 | else
31 | {
32 | tcs.SetResult((int)eventArgs.Result);
33 | }
34 | };
35 |
36 | worker.RunWorkerAsync();
37 |
38 | int result = tcs.Task.Result;
39 |
40 | Console.WriteLine("Result is: {0}", result);
41 | }
42 |
43 | static int TaskMethod(string name, int seconds)
44 | {
45 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
46 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
47 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
48 | return 42 * seconds;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/Chapter4/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("36711db7-2242-4a17-8332-a8be103a94bb")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe6/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter4.Recipe6
6 | {
7 | class Program
8 | {
9 | private static void Main(string[] args)
10 | {
11 | var cts = new CancellationTokenSource();
12 | var longTask = new Task(() => TaskMethod("Task 1", 10, cts.Token), cts.Token);
13 | Console.WriteLine(longTask.Status);
14 | cts.Cancel();
15 | Console.WriteLine(longTask.Status);
16 | Console.WriteLine("First task has been cancelled before execution");
17 | cts = new CancellationTokenSource();
18 | longTask = new Task(() => TaskMethod("Task 2", 10, cts.Token), cts.Token);
19 | longTask.Start();
20 | for (int i = 0; i < 5; i++ )
21 | {
22 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
23 | Console.WriteLine(longTask.Status);
24 | }
25 | cts.Cancel();
26 | for (int i = 0; i < 5; i++)
27 | {
28 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
29 | Console.WriteLine(longTask.Status);
30 | }
31 |
32 | Console.WriteLine("A task has been completed with result {0}.", longTask.Result);
33 | }
34 |
35 | private static int TaskMethod(string name, int seconds, CancellationToken token)
36 | {
37 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
38 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
39 | for (int i = 0; i < seconds; i ++)
40 | {
41 | Thread.Sleep(TimeSpan.FromSeconds(1));
42 | if (token.IsCancellationRequested) return -1;
43 | }
44 | return 42*seconds;
45 | }
46 | }
47 | }
--------------------------------------------------------------------------------
/Chapter4/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8b37495d-c8c5-458c-85c3-4fdb1759f1fa")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe7/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter4.Recipe7
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task task;
12 | try
13 | {
14 | task = Task.Run(() => TaskMethod("Task 1", 2));
15 | int result = task.Result;
16 | Console.WriteLine("Result: {0}", result);
17 | }
18 | catch (Exception ex)
19 | {
20 | Console.WriteLine("Exception caught: {0}", ex);
21 | }
22 | Console.WriteLine("----------------------------------------------");
23 | Console.WriteLine();
24 |
25 | try
26 | {
27 | task = Task.Run(() => TaskMethod("Task 2", 2));
28 | int result = task.GetAwaiter().GetResult();
29 | Console.WriteLine("Result: {0}", result);
30 | }
31 | catch (Exception ex)
32 | {
33 | Console.WriteLine("Exception caught: {0}", ex);
34 | }
35 | Console.WriteLine("----------------------------------------------");
36 | Console.WriteLine();
37 |
38 | var t1 = new Task(() => TaskMethod("Task 3", 3));
39 | var t2 = new Task(() => TaskMethod("Task 4", 2));
40 | var complexTask = Task.WhenAll(t1, t2);
41 | var exceptionHandler = complexTask.ContinueWith(t =>
42 | Console.WriteLine("Exception caught: {0}", t.Exception),
43 | TaskContinuationOptions.OnlyOnFaulted
44 | );
45 | t1.Start();
46 | t2.Start();
47 |
48 | Thread.Sleep(TimeSpan.FromSeconds(5));
49 | }
50 |
51 | static int TaskMethod(string name, int seconds)
52 | {
53 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
54 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
55 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
56 | throw new Exception("Boom!");
57 | return 42 * seconds;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Chapter4/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("018efd13-0ef9-44ae-beca-6ad1ded1cd3a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe8/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter4.Recipe8
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var firstTask = new Task(() => TaskMethod("First Task", 3));
13 | var secondTask = new Task(() => TaskMethod("Second Task", 2));
14 | var whenAllTask = Task.WhenAll(firstTask, secondTask);
15 |
16 | whenAllTask.ContinueWith(t =>
17 | Console.WriteLine("The first answer is {0}, the second is {1}", t.Result[0], t.Result[1]),
18 | TaskContinuationOptions.OnlyOnRanToCompletion
19 | );
20 |
21 | firstTask.Start();
22 | secondTask.Start();
23 |
24 | Thread.Sleep(TimeSpan.FromSeconds(4));
25 |
26 | var tasks = new List>();
27 | for (int i = 1; i < 4; i++)
28 | {
29 | int counter = i;
30 | var task = new Task(() => TaskMethod(string.Format("Task {0}", counter), counter));
31 | tasks.Add(task);
32 | task.Start();
33 | }
34 |
35 | while (tasks.Count > 0)
36 | {
37 | var completedTask = Task.WhenAny(tasks).Result;
38 | tasks.Remove(completedTask);
39 | Console.WriteLine("A task has been completed with result {0}.", completedTask.Result);
40 | }
41 |
42 | Thread.Sleep(TimeSpan.FromSeconds(1));
43 | }
44 |
45 | static int TaskMethod(string name, int seconds)
46 | {
47 | Console.WriteLine("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
48 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
49 | Thread.Sleep(TimeSpan.FromSeconds(seconds));
50 | return 42 * seconds;
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Chapter4/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f066e26e-90b8-4fde-ad3c-384a787292ae")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/App.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 |
3 | namespace Chapter4.Recipe9
4 | {
5 | ///
6 | /// Interaction logic for App.xaml
7 | ///
8 | public partial class App : Application
9 | {
10 | }
11 | }
--------------------------------------------------------------------------------
/Chapter4/Recipe9/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.18033
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace Chapter4.Recipe9.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter5/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter5.Recipe1
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task t = AsynchronyWithTPL();
12 | t.Wait();
13 |
14 | t = AsynchronyWithAwait();
15 | t.Wait();
16 | }
17 |
18 | static Task AsynchronyWithTPL()
19 | {
20 | Task t = GetInfoAsync("Task 1");
21 | Task t2 = t.ContinueWith(task => Console.WriteLine(t.Result),
22 | TaskContinuationOptions.NotOnFaulted);
23 | Task t3 = t.ContinueWith(task => Console.WriteLine(t.Exception.InnerException),
24 | TaskContinuationOptions.OnlyOnFaulted);
25 |
26 | return Task.WhenAny(t2, t3);
27 | }
28 |
29 | async static Task AsynchronyWithAwait()
30 | {
31 | try
32 | {
33 | string result = await GetInfoAsync("Task 2");
34 | Console.WriteLine(result);
35 | }
36 | catch (Exception ex)
37 | {
38 | Console.WriteLine(ex);
39 | }
40 | }
41 |
42 | async static Task GetInfoAsync(string name)
43 | {
44 | await Task.Delay(TimeSpan.FromSeconds(2));
45 | //throw new Exception("Boom!");
46 | return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
47 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter5/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("8fcf91d8-0198-4221-b29e-5c632838d4e6")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter5.Recipe2
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task t = AsynchronousProcessing();
12 | t.Wait();
13 | }
14 |
15 | async static Task AsynchronousProcessing()
16 | {
17 | Func> asyncLambda = async name => {
18 | await Task.Delay(TimeSpan.FromSeconds(2));
19 | return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
20 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
21 | };
22 |
23 | string result = await asyncLambda("async lambda");
24 |
25 | Console.WriteLine(result);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter5/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("77b30514-57ed-40b4-975e-301abe91d892")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter5.Recipe3
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task t = AsynchronyWithTPL();
12 | t.Wait();
13 |
14 | t = AsynchronyWithAwait();
15 | t.Wait();
16 | }
17 |
18 | static Task AsynchronyWithTPL()
19 | {
20 | var containerTask = new Task(() => {
21 | Task t = GetInfoAsync("TPL 1");
22 | t.ContinueWith(task => {
23 | Console.WriteLine(t.Result);
24 | Task t2 = GetInfoAsync("TPL 2");
25 | t2.ContinueWith(innerTask => Console.WriteLine(innerTask.Result),
26 | TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.AttachedToParent);
27 | t2.ContinueWith(innerTask => Console.WriteLine(innerTask.Exception.InnerException),
28 | TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent);
29 | },
30 | TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.AttachedToParent);
31 |
32 | t.ContinueWith(task => Console.WriteLine(t.Exception.InnerException),
33 | TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent);
34 | });
35 |
36 | containerTask.Start();
37 | return containerTask;
38 | }
39 |
40 | async static Task AsynchronyWithAwait()
41 | {
42 | try
43 | {
44 | string result = await GetInfoAsync("Async 1");
45 | Console.WriteLine(result);
46 | result = await GetInfoAsync("Async 2");
47 | Console.WriteLine(result);
48 | }
49 | catch (Exception ex)
50 | {
51 | Console.WriteLine(ex);
52 | }
53 | }
54 |
55 | async static Task GetInfoAsync(string name)
56 | {
57 | Console.WriteLine("Task {0} started!", name);
58 | await Task.Delay(TimeSpan.FromSeconds(2));
59 | if(name == "TPL 2")
60 | throw new Exception("Boom!");
61 | return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
62 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/Chapter5/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("de0aa5eb-4ed2-436d-8c8d-40fe8f2915d0")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter5.Recipe4
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task t = AsynchronousProcessing();
12 | t.Wait();
13 | }
14 |
15 | async static Task AsynchronousProcessing()
16 | {
17 | Task t1 = GetInfoAsync("Task 1", 3);
18 | Task t2 = GetInfoAsync("Task 2", 5);
19 |
20 | string[] results = await Task.WhenAll(t1, t2);
21 | foreach (string result in results)
22 | {
23 | Console.WriteLine(result);
24 | }
25 | }
26 |
27 | async static Task GetInfoAsync(string name, int seconds)
28 | {
29 | await Task.Delay(TimeSpan.FromSeconds(seconds));
30 | //await Task.Run(() => Thread.Sleep(TimeSpan.FromSeconds(seconds)));
31 | return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
32 | name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Chapter5/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("82891c1e-efbf-490e-8160-65bcc9bfd3ce")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 |
5 | namespace Chapter5.Recipe5
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Task t = AsynchronousProcessing();
12 | t.Wait();
13 | }
14 |
15 | async static Task AsynchronousProcessing()
16 | {
17 | Console.WriteLine("1. Single exception");
18 |
19 | try
20 | {
21 | string result = await GetInfoAsync("Task 1", 2);
22 | Console.WriteLine(result);
23 | }
24 | catch (Exception ex)
25 | {
26 | Console.WriteLine("Exception details: {0}", ex);
27 | }
28 |
29 | Console.WriteLine();
30 | Console.WriteLine("2. Multiple exceptions");
31 |
32 | Task t1 = GetInfoAsync("Task 1", 3);
33 | Task t2 = GetInfoAsync("Task 2", 2);
34 | try
35 | {
36 | string[] results = await Task.WhenAll(t1, t2);
37 | Console.WriteLine(results.Length);
38 | }
39 | catch (Exception ex)
40 | {
41 | Console.WriteLine("Exception details: {0}", ex);
42 | }
43 |
44 | Console.WriteLine();
45 | Console.WriteLine("2. Multiple exceptions with AggregateException");
46 |
47 | t1 = GetInfoAsync("Task 1", 3);
48 | t2 = GetInfoAsync("Task 2", 2);
49 | Task t3 = Task.WhenAll(t1, t2);
50 | try
51 | {
52 | string[] results = await t3;
53 | Console.WriteLine(results.Length);
54 | }
55 | catch
56 | {
57 | var ae = t3.Exception.Flatten();
58 | var exceptions = ae.InnerExceptions;
59 | Console.WriteLine("Exceptions caught: {0}", exceptions.Count);
60 | foreach (var e in exceptions)
61 | {
62 | Console.WriteLine("Exception details: {0}", e);
63 | Console.WriteLine();
64 | }
65 | }
66 | }
67 |
68 | async static Task GetInfoAsync(string name, int seconds)
69 | {
70 | await Task.Delay(TimeSpan.FromSeconds(seconds));
71 | throw new Exception(string.Format("Boom from {0}!", name));
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Chapter5/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("810f3323-5b49-42b9-8c0b-70fe047c5256")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe6/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/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("6c3f3d01-ae29-4e41-bc54-0bb19ef212bd")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe7/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/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("67a9fce3-a47d-4258-90db-7c12a7ee1b92")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe8/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.CompilerServices;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter5.Recipe8
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsynchronousProcessing();
13 | t.Wait();
14 | }
15 |
16 | async static Task AsynchronousProcessing()
17 | {
18 | var sync = new CustomAwaitable(true);
19 | string result = await sync;
20 | Console.WriteLine(result);
21 |
22 | var async = new CustomAwaitable(false);
23 | result = await async;
24 |
25 | Console.WriteLine(result);
26 | }
27 |
28 | class CustomAwaitable
29 | {
30 | public CustomAwaitable(bool completeSynchronously)
31 | {
32 | _completeSynchronously = completeSynchronously;
33 | }
34 |
35 | public CustomAwaiter GetAwaiter()
36 | {
37 | return new CustomAwaiter(_completeSynchronously);
38 | }
39 |
40 | private readonly bool _completeSynchronously;
41 | }
42 |
43 | class CustomAwaiter : INotifyCompletion
44 | {
45 | private string _result = "Completed synchronously";
46 | private readonly bool _completeSynchronously;
47 |
48 | public bool IsCompleted { get { return _completeSynchronously; } }
49 |
50 | public CustomAwaiter(bool completeSynchronously)
51 | {
52 | _completeSynchronously = completeSynchronously;
53 | }
54 |
55 | public string GetResult()
56 | {
57 | return _result;
58 | }
59 |
60 | public void OnCompleted(Action continuation)
61 | {
62 | ThreadPool.QueueUserWorkItem( state => {
63 | Thread.Sleep(TimeSpan.FromSeconds(1));
64 | _result = GetInfo();
65 | if (continuation != null) continuation();
66 | });
67 | }
68 |
69 | private string GetInfo()
70 | {
71 | return string.Format("Task is running on a thread id {0}. Is thread pool thread: {1}",
72 | Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/Chapter5/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("bb78cc1c-d0f6-4816-8aa2-060ea6e0681f")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe9/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter5/Recipe9/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Dynamic;
3 | using System.Runtime.CompilerServices;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 | using ImpromptuInterface;
7 |
8 | namespace Chapter5.Recipe9
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | Task t = AsynchronousProcessing();
15 | t.Wait();
16 | }
17 |
18 | async static Task AsynchronousProcessing()
19 | {
20 | string result = await GetDynamicAwaitableObject(true);
21 | Console.WriteLine(result);
22 |
23 | result = await GetDynamicAwaitableObject(false);
24 | Console.WriteLine(result);
25 | }
26 |
27 | static dynamic GetDynamicAwaitableObject(bool completeSynchronously)
28 | {
29 | dynamic result = new ExpandoObject();
30 | dynamic awaiter = new ExpandoObject();
31 |
32 | awaiter.Message = "Completed synchronously";
33 | awaiter.IsCompleted = completeSynchronously;
34 | awaiter.GetResult = (Func)(() => awaiter.Message);
35 |
36 | awaiter.OnCompleted = (Action) ( callback =>
37 | ThreadPool.QueueUserWorkItem(state => {
38 | Thread.Sleep(TimeSpan.FromSeconds(1));
39 | awaiter.Message = GetInfo();
40 | if (callback != null) callback();
41 | })
42 | );
43 |
44 | IAwaiter proxy = Impromptu.ActLike(awaiter);
45 |
46 | result.GetAwaiter = (Func) ( () => proxy );
47 |
48 | return result;
49 | }
50 |
51 | static string GetInfo()
52 | {
53 | return string.Format("Task is running on a thread id {0}. Is thread pool thread: {1}",
54 | Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
55 | }
56 | }
57 |
58 | public interface IAwaiter : INotifyCompletion
59 | {
60 | bool IsCompleted { get; }
61 |
62 | T GetResult();
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Chapter5/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("ca606361-8878-439f-933b-f44deca6e70f")]
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 |
--------------------------------------------------------------------------------
/Chapter5/Recipe9/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter6/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter6/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Collections.Generic;
4 | using System.Diagnostics;
5 |
6 | namespace Chapter6.Recipe1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var concurrentDictionary = new ConcurrentDictionary();
13 | var dictionary = new Dictionary();
14 |
15 | var sw = new Stopwatch();
16 |
17 | sw.Start();
18 | for (int i = 0; i < 1000000; i++)
19 | {
20 | lock (dictionary)
21 | {
22 | dictionary[i] = Item;
23 | }
24 | }
25 | sw.Stop();
26 | Console.WriteLine("Writing to dictionary with a lock: {0}", sw.Elapsed);
27 |
28 | sw.Restart();
29 | for (int i = 0; i < 1000000; i++)
30 | {
31 | concurrentDictionary[i] = Item;
32 | }
33 | sw.Stop();
34 | Console.WriteLine("Writing to a concurrent dictionary: {0}", sw.Elapsed);
35 |
36 | sw.Restart();
37 | for (int i = 0; i < 1000000; i++)
38 | {
39 | lock (dictionary)
40 | {
41 | CurrentItem = dictionary[i];
42 | }
43 | }
44 | sw.Stop();
45 | Console.WriteLine("Reading from dictionary with a lock: {0}", sw.Elapsed);
46 |
47 | sw.Restart();
48 | for (int i = 0; i < 1000000; i++)
49 | {
50 | CurrentItem = concurrentDictionary[i];
51 | }
52 | sw.Stop();
53 | Console.WriteLine("Reading from a concurrent dictionary: {0}", sw.Elapsed);
54 | }
55 |
56 | const string Item = "Dictionary item";
57 | public static string CurrentItem;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Chapter6/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("1f580e5d-42b1-4f3d-8f4f-861515a72392")]
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 |
--------------------------------------------------------------------------------
/Chapter6/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter6/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter6.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = RunProgram();
13 | t.Wait();
14 | }
15 |
16 | static async Task RunProgram()
17 | {
18 | var taskQueue = new ConcurrentQueue();
19 | var cts = new CancellationTokenSource();
20 |
21 | var taskSource = Task.Run(() => TaskProducer(taskQueue));
22 |
23 | Task[] processors = new Task[4];
24 | for (int i = 1; i <= 4; i++)
25 | {
26 | string processorId = i.ToString();
27 | processors[i-1] = Task.Run(
28 | () => TaskProcessor(taskQueue, "Processor " + processorId, cts.Token));
29 | }
30 |
31 | await taskSource;
32 | cts.CancelAfter(TimeSpan.FromSeconds(2));
33 |
34 | await Task.WhenAll(processors);
35 | }
36 |
37 | static async Task TaskProducer(ConcurrentQueue queue)
38 | {
39 | for (int i = 1; i <= 20; i++)
40 | {
41 | await Task.Delay(50);
42 | var workItem = new CustomTask {Id = i};
43 | queue.Enqueue(workItem);
44 | Console.WriteLine("Task {0} has been posted", workItem.Id);
45 | }
46 | }
47 |
48 | static async Task TaskProcessor(
49 | ConcurrentQueue queue, string name, CancellationToken token)
50 | {
51 | CustomTask workItem;
52 | bool dequeueSuccesful = false;
53 |
54 | await GetRandomDelay();
55 | do
56 | {
57 | dequeueSuccesful = queue.TryDequeue(out workItem);
58 | if (dequeueSuccesful)
59 | {
60 | Console.WriteLine("Task {0} has been processed by {1}", workItem.Id, name);
61 | }
62 |
63 | await GetRandomDelay();
64 | }
65 | while (!token.IsCancellationRequested);
66 | }
67 |
68 | static Task GetRandomDelay()
69 | {
70 | int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
71 | return Task.Delay(delay);
72 | }
73 |
74 | class CustomTask
75 | {
76 | public int Id { get; set; }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/Chapter6/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("482c8161-819c-4fc0-9bb4-1f1aa63b97a4")]
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 |
--------------------------------------------------------------------------------
/Chapter6/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter6/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter6.Recipe3
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = RunProgram();
13 | t.Wait();
14 | }
15 |
16 | static async Task RunProgram()
17 | {
18 | var taskStack = new ConcurrentStack();
19 | var cts = new CancellationTokenSource();
20 |
21 | var taskSource = Task.Run(() => TaskProducer(taskStack));
22 |
23 | Task[] processors = new Task[4];
24 | for (int i = 1; i <= 4; i++)
25 | {
26 | string processorId = i.ToString();
27 | processors[i - 1] = Task.Run(
28 | () => TaskProcessor(taskStack, "Processor " + processorId, cts.Token));
29 | }
30 |
31 | await taskSource;
32 | cts.CancelAfter(TimeSpan.FromSeconds(2));
33 |
34 | await Task.WhenAll(processors);
35 | }
36 |
37 | static async Task TaskProducer(ConcurrentStack stack)
38 | {
39 | for (int i = 1; i <= 20; i++)
40 | {
41 | await Task.Delay(50);
42 | var workItem = new CustomTask { Id = i };
43 | stack.Push(workItem);
44 | Console.WriteLine("Task {0} has been posted", workItem.Id);
45 | }
46 | }
47 |
48 | static async Task TaskProcessor(
49 | ConcurrentStack stack, string name, CancellationToken token)
50 | {
51 | await GetRandomDelay();
52 | do
53 | {
54 | CustomTask workItem;
55 | bool popSuccesful = stack.TryPop(out workItem);
56 | if (popSuccesful)
57 | {
58 | Console.WriteLine("Task {0} has been processed by {1}", workItem.Id, name);
59 | }
60 |
61 | await GetRandomDelay();
62 | }
63 | while (!token.IsCancellationRequested);
64 | }
65 |
66 | static Task GetRandomDelay()
67 | {
68 | int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
69 | return Task.Delay(delay);
70 | }
71 |
72 | class CustomTask
73 | {
74 | public int Id { get; set; }
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/Chapter6/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("c7834167-b9c3-468c-ac86-25affca030a4")]
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 |
--------------------------------------------------------------------------------
/Chapter6/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter6/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("efc6c0cd-4b43-42a1-8231-f979f44116eb")]
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 |
--------------------------------------------------------------------------------
/Chapter6/Recipe5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter6/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("7af88e53-d78b-4a78-8fd7-ac1c9fb326e0")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 |
6 | namespace Chapter7.Recipe1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Parallel.Invoke(
13 | () => EmulateProcessing("Task1"),
14 | () => EmulateProcessing("Task2"),
15 | () => EmulateProcessing("Task3")
16 | );
17 |
18 | var cts = new CancellationTokenSource();
19 |
20 | var result = Parallel.ForEach(
21 | Enumerable.Range(1, 30),
22 | new ParallelOptions
23 | {
24 | CancellationToken = cts.Token,
25 | MaxDegreeOfParallelism = Environment.ProcessorCount,
26 | TaskScheduler = TaskScheduler.Default
27 | },
28 | (i, state) =>
29 | {
30 | Console.WriteLine(i);
31 | if (i == 20)
32 | {
33 | state.Break();
34 | Console.WriteLine("Loop is stopped: {0}", state.IsStopped);
35 | }
36 | });
37 |
38 | Console.WriteLine("---");
39 | Console.WriteLine("IsCompleted: {0}", result.IsCompleted);
40 | Console.WriteLine("Lowest break iteration: {0}", result.LowestBreakIteration);
41 | }
42 |
43 | static string EmulateProcessing(string taskName)
44 | {
45 | Thread.Sleep(TimeSpan.FromMilliseconds(
46 | new Random(DateTime.Now.Millisecond).Next(250, 350)));
47 | Console.WriteLine("{0} task was processed on a thread id {1}",
48 | taskName, Thread.CurrentThread.ManagedThreadId);
49 | return taskName;
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter7/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("f6f44212-5ade-4065-a59a-e9d0b19e7f6f")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/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("fb50a454-6b5a-4984-9a60-2e221797b969")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/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("67d01f50-0bf8-4d0a-a614-6687fd3e1154")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | namespace Chapter7.Recipe4
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | IEnumerable numbers = Enumerable.Range(-5, 10);
12 |
13 | var query = from number in numbers
14 | select 100 / number;
15 |
16 | try
17 | {
18 | foreach(var n in query)
19 | Console.WriteLine(n);
20 | }
21 | catch (DivideByZeroException)
22 | {
23 | Console.WriteLine("Divided by zero!");
24 | }
25 |
26 | Console.WriteLine("---");
27 | Console.WriteLine("Sequential LINQ query processing");
28 | Console.WriteLine();
29 |
30 | var parallelQuery = from number in numbers.AsParallel()
31 | select 100 / number; ;
32 |
33 | try
34 | {
35 | parallelQuery.ForAll(Console.WriteLine);
36 | }
37 | catch (DivideByZeroException)
38 | {
39 | Console.WriteLine("Divided by zero - usual exception handler!");
40 | }
41 | catch (AggregateException e)
42 | {
43 | e.Flatten().Handle(ex =>
44 | {
45 | if (ex is DivideByZeroException)
46 | {
47 | Console.WriteLine("Divided by zero - aggregate exception handler!");
48 | return true;
49 | }
50 |
51 | return false;
52 | });
53 | }
54 |
55 | Console.WriteLine("---");
56 | Console.WriteLine("Parallel LINQ query processing and results merging");
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Chapter7/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("c3fe80f1-0a25-4647-a374-376e4d38a0db")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/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("ad42b7d5-cbbd-4e06-bd6d-3e4c593ff2ba")]
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 |
--------------------------------------------------------------------------------
/Chapter7/Recipe6/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter7/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("8033e27d-503e-4d4a-8e1a-5727f1048681")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reactive.Concurrency;
4 | using System.Reactive.Linq;
5 | using System.Threading;
6 |
7 | namespace Chapter8.Recipe1
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | foreach (int i in EnumerableEventSequence())
14 | {
15 | Console.Write(i);
16 | }
17 | Console.WriteLine();
18 | Console.WriteLine("IEnumerable");
19 |
20 | IObservable o = EnumerableEventSequence().ToObservable();
21 | using (IDisposable subscription = o.Subscribe(Console.Write))
22 | {
23 | Console.WriteLine();
24 | Console.WriteLine("IObservable");
25 | }
26 |
27 | o = EnumerableEventSequence().ToObservable().SubscribeOn(TaskPoolScheduler.Default);
28 | using (IDisposable subscription = o.Subscribe(Console.Write))
29 | {
30 | Console.WriteLine();
31 | Console.WriteLine("IObservable async");
32 | Console.ReadLine();
33 | }
34 | }
35 |
36 | static IEnumerable EnumerableEventSequence()
37 | {
38 | for (int i = 0; i < 10; i++)
39 | {
40 | Thread.Sleep(TimeSpan.FromSeconds(0.5));
41 | yield return i;
42 | }
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/Chapter8/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("a8776703-d6da-4671-9add-bd574af7ad0b")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe1/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter8/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reactive.Concurrency;
4 | using System.Reactive.Disposables;
5 | using System.Reactive.Linq;
6 | using System.Threading;
7 |
8 | namespace Chapter8.Recipe2
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | var observer = new CustomObserver();
15 |
16 | var goodObservable = new CustomSequence(new[] {1, 2, 3, 4, 5});
17 | var badObservable = new CustomSequence(null);
18 |
19 | using (IDisposable subscription = goodObservable.Subscribe(observer))
20 | {
21 | }
22 |
23 | using (IDisposable subscription = goodObservable.SubscribeOn(TaskPoolScheduler.Default).Subscribe(observer))
24 | {
25 | Thread.Sleep(TimeSpan.FromMilliseconds(100));
26 | Console.WriteLine("Press ENTER to continue");
27 | Console.ReadLine();
28 | }
29 |
30 | using (IDisposable subscription = badObservable.SubscribeOn(TaskPoolScheduler.Default).Subscribe(observer))
31 | {
32 | Thread.Sleep(TimeSpan.FromMilliseconds(100));
33 | Console.WriteLine("Press ENTER to continue");
34 | Console.ReadLine();
35 | }
36 | }
37 |
38 | class CustomObserver : IObserver
39 | {
40 | public void OnNext(int value)
41 | {
42 | Console.WriteLine("Next value: {0}; Thread Id: {1}", value, Thread.CurrentThread.ManagedThreadId);
43 | }
44 |
45 | public void OnError(Exception error)
46 | {
47 | Console.WriteLine("Error: {0}", error.Message);
48 | }
49 |
50 | public void OnCompleted()
51 | {
52 | Console.WriteLine("Completed");
53 | }
54 | }
55 |
56 | class CustomSequence : IObservable
57 | {
58 | private readonly IEnumerable _numbers;
59 |
60 | public CustomSequence(IEnumerable numbers)
61 | {
62 | _numbers = numbers;
63 | }
64 | public IDisposable Subscribe(IObserver observer)
65 | {
66 | foreach (var number in _numbers)
67 | {
68 | observer.OnNext(number);
69 | }
70 | observer.OnCompleted();
71 | return Disposable.Empty;
72 | }
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Chapter8/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("a1968e83-3858-4afb-9c55-08f7f5a2d0cc")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe2/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter8/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/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("ef76f016-70a3-4e8c-ad21-1f693817c13b")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe3/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter8/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/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("b4d4b0b0-8ec2-4ce0-91ad-4d5738732c9c")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe4/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter8/Recipe5/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reactive.Linq;
3 |
4 | namespace Chapter8.Recipe5
5 | {
6 | class Program
7 | {
8 | static void Main(string[] args)
9 | {
10 | IObservable sequence = Observable.Interval(TimeSpan.FromMilliseconds(50)).Take(21);
11 |
12 | var evenNumbers = from n in sequence
13 | where n % 2 == 0
14 | select n;
15 |
16 | var oddNumbers = from n in sequence
17 | where n % 2 != 0
18 | select n;
19 |
20 | var combine = from n in evenNumbers.Concat(oddNumbers)
21 | select n;
22 |
23 | var nums = (from n in combine
24 | where n % 5 == 0
25 | select n)
26 | .Do(n => Console.WriteLine("------Number {0} is processed in Do method", n));
27 |
28 | using (var sub = OutputToConsole(sequence, 0))
29 | using (var sub2 = OutputToConsole(combine, 1))
30 | using (var sub3 = OutputToConsole(nums, 2))
31 | {
32 | Console.WriteLine("Press enter to finish the demo");
33 | Console.ReadLine();
34 | }
35 | }
36 |
37 | static IDisposable OutputToConsole(IObservable sequence, int innerLevel)
38 | {
39 | string delimiter = innerLevel == 0 ? string.Empty : new string('-', innerLevel*3);
40 | return sequence.Subscribe(
41 | obj => Console.WriteLine("{0}{1}", delimiter, obj)
42 | , ex => Console.WriteLine("Error: {0}", ex.Message)
43 | , () => Console.WriteLine("{0}Completed", delimiter)
44 | );
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Chapter8/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("bf8c7f7e-8ad1-4aea-b9c0-26a1bb288522")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe5/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter8/Recipe6/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter8/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("1b968df8-7d7a-43bf-b4ed-f9802ddac45c")]
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 |
--------------------------------------------------------------------------------
/Chapter8/Recipe6/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/Chapter9/Recipe1/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter9/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("6f82f6bb-096d-4d53-80dd-918752720b78")]
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 |
--------------------------------------------------------------------------------
/Chapter9/Recipe2/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter9/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("2040a96b-a6b3-4c8b-890e-1cf0494e12a2")]
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 |
--------------------------------------------------------------------------------
/Chapter9/Recipe3/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter9/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("08138732-0c5b-4556-8626-8e98d9d26269")]
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 |
--------------------------------------------------------------------------------
/Chapter9/Recipe4/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Chapter9/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("17030e60-a3a3-4bbd-9577-3664c2e48f5a")]
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 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 Eugene Agafonov
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Code samples for Multithreading in C# 5.0 Cookbook
2 | ======================
3 |
4 | Here are the code samples from my book Multithreading in C# 5.0 Cookbook, converted to Visual Studio 2013 and Windows 8.1
5 | You can use Visual Studio Express 2013 for Windows Desktop to open most of the recipes, and Visual Studio Express 2013 for Windows to open Windows Store samples from Chapter 11. Just open the corresponging .sln file.
6 |
7 | In case you have any suggestions or bug reports please send them to multithreading@eugeneagafonov.com or post a comments to my blog post
8 |
--------------------------------------------------------------------------------
/test/test.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Express 2013 for Windows Desktop
4 | VisualStudioVersion = 12.0.21005.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Recipe2", "..\Chapter11\Recipe2\Recipe2.csproj", "{DEA67BEA-863F-40C5-A220-0D2DB4BD73FB}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {DEA67BEA-863F-40C5-A220-0D2DB4BD73FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {DEA67BEA-863F-40C5-A220-0D2DB4BD73FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {DEA67BEA-863F-40C5-A220-0D2DB4BD73FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {DEA67BEA-863F-40C5-A220-0D2DB4BD73FB}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------