├── .gitattributes
├── .gitignore
├── BookSamples_VisualStudio2015.sln
├── Chapter1
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe10
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe10.csproj
├── Recipe11
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe11.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe9.csproj
├── Chapter10
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe3.csproj
│ └── packages.config
└── Recipe4
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Recipe4.csproj
│ └── packages.config
├── Chapter11
├── Recipe1
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── ApplicationInsights.config
│ ├── Assets
│ │ ├── LockScreenLogo.scale-200.png
│ │ ├── SplashScreen.scale-200.png
│ │ ├── Square150x150Logo.scale-200.png
│ │ ├── Square44x44Logo.scale-200.png
│ │ ├── Square44x44Logo.targetsize-24_altform-unplated.png
│ │ ├── StoreLogo.png
│ │ └── Wide310x150Logo.scale-200.png
│ ├── MainPage.xaml
│ ├── MainPage.xaml.cs
│ ├── Package.appxmanifest
│ ├── Properties
│ │ ├── AssemblyInfo.cs
│ │ └── Default.rd.xml
│ ├── Recipe1.csproj
│ ├── Recipe1_TemporaryKey.pfx
│ └── project.json
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── ApplicationInsights.config
│ ├── Assets
│ │ ├── LockScreenLogo.scale-200.png
│ │ ├── SplashScreen.scale-200.png
│ │ ├── Square150x150Logo.scale-200.png
│ │ ├── Square44x44Logo.scale-200.png
│ │ ├── Square44x44Logo.targetsize-24_altform-unplated.png
│ │ ├── StoreLogo.png
│ │ └── Wide310x150Logo.scale-200.png
│ ├── MainPage.xaml
│ ├── MainPage.xaml.cs
│ ├── Package.appxmanifest
│ ├── Properties
│ │ ├── AssemblyInfo.cs
│ │ └── Default.rd.xml
│ ├── Recipe3.csproj
│ ├── Recipe3_TemporaryKey.pfx
│ └── project.json
├── Recipe4
│ ├── Program.cs
│ └── dotnetcore_installation.txt
└── Recipe5
│ ├── Program.cs
│ └── dotnetcore_installation.txt
├── Chapter2
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe9.csproj
├── Chapter3
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
└── Recipe7
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Chapter4
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── MainWindow.xaml
│ ├── MainWindow.xaml.cs
│ ├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
│ └── Recipe9.csproj
├── Chapter5
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Recipe6
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Recipe7
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe7.csproj
├── Recipe8
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe8.csproj
└── Recipe9
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Recipe9.csproj
│ └── packages.config
├── Chapter6
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
└── Recipe5
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe5.csproj
├── Chapter7
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe5.csproj
└── Recipe6
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe6.csproj
├── Chapter8
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe1.csproj
│ └── packages.config
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe2.csproj
│ └── packages.config
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe3.csproj
│ └── packages.config
├── Recipe4
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe4.csproj
│ └── packages.config
├── Recipe5
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ ├── Recipe5.csproj
│ └── packages.config
└── Recipe6
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── Recipe6.csproj
│ └── packages.config
├── Chapter9
├── Recipe1
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe1.csproj
├── Recipe2
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe2.csproj
├── Recipe3
│ ├── Program.cs
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── Recipe3.csproj
└── Recipe4
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ └── Recipe4.csproj
├── LICENSE
└── README.md
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 |
7 | # Standard to msysgit
8 | *.doc diff=astextplain
9 | *.DOC diff=astextplain
10 | *.docx diff=astextplain
11 | *.DOCX diff=astextplain
12 | *.dot diff=astextplain
13 | *.DOT diff=astextplain
14 | *.pdf diff=astextplain
15 | *.PDF diff=astextplain
16 | *.rtf diff=astextplain
17 | *.RTF diff=astextplain
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Windows image file caches
2 | Thumbs.db
3 | ehthumbs.db
4 |
5 | # Folder config file
6 | Desktop.ini
7 |
8 | # Recycle Bin used on file shares
9 | $RECYCLE.BIN/
10 |
11 | # Windows Installer files
12 | *.cab
13 | *.msi
14 | *.msm
15 | *.msp
16 |
17 | # Windows shortcuts
18 | *.lnk
19 |
20 | # =========================
21 | # Operating System Files
22 | # =========================
23 |
24 | # OSX
25 | # =========================
26 |
27 | .DS_Store
28 | .AppleDouble
29 | .LSOverride
30 |
31 | # Thumbnails
32 | ._*
33 |
34 | # Files that might appear in the root of a volume
35 | .DocumentRevisions-V100
36 | .fseventsd
37 | .Spotlight-V100
38 | .TemporaryItems
39 | .Trashes
40 | .VolumeIcon.icns
41 |
42 | # Directories potentially created on remote AFP share
43 | .AppleDB
44 | .AppleDesktop
45 | Network Trash Folder
46 | Temporary Items
47 | .apdisk
48 |
--------------------------------------------------------------------------------
/Chapter1/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 |
5 | namespace Chapter1.Recipe1
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | Thread t = new Thread(PrintNumbers);
12 | t.Start();
13 | PrintNumbers();
14 | }
15 |
16 | static void PrintNumbers()
17 | {
18 | WriteLine("Starting...");
19 | for (int i = 1; i < 10; i++)
20 | {
21 | WriteLine(i);
22 | }
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Chapter1/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("0ceeb3db-93f2-4154-9013-d777321f3f17")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe10/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe10
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | object lock1 = new object();
13 | object lock2 = new object();
14 |
15 | new Thread(() => LockTooMuch(lock1, lock2)).Start();
16 |
17 | lock (lock2)
18 | {
19 | Thread.Sleep(1000);
20 | WriteLine("Monitor.TryEnter allows not to get stuck, returning false after a specified timeout is elapsed");
21 | if (Monitor.TryEnter(lock1, TimeSpan.FromSeconds(5)))
22 | {
23 | WriteLine("Acquired a protected resource succesfully");
24 | }
25 | else
26 | {
27 | WriteLine("Timeout acquiring a resource!");
28 | }
29 | }
30 |
31 | new Thread(() => LockTooMuch(lock1, lock2)).Start();
32 |
33 | WriteLine("----------------------------------");
34 | lock (lock2)
35 | {
36 | WriteLine("This will be a deadlock!");
37 | Sleep(1000);
38 | lock (lock1)
39 | {
40 | WriteLine("Acquired a protected resource succesfully");
41 | }
42 | }
43 | }
44 |
45 | static void LockTooMuch(object lock1, object lock2)
46 | {
47 | lock (lock1)
48 | {
49 | Sleep(1000);
50 | lock (lock2);
51 | }
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter1/Recipe10/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe10")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe10")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("27746d59-c51e-4c29-a543-f9b3255fbc9d")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe11/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe11
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t = new Thread(FaultyThread);
13 | t.Start();
14 | t.Join();
15 |
16 | try
17 | {
18 | t = new Thread(BadFaultyThread);
19 | t.Start();
20 | }
21 | catch (Exception ex)
22 | {
23 | WriteLine("We won't get here!");
24 | }
25 | }
26 |
27 | static void BadFaultyThread()
28 | {
29 | WriteLine("Starting a faulty thread...");
30 | Sleep(TimeSpan.FromSeconds(2));
31 | throw new Exception("Boom!");
32 | }
33 |
34 | static void FaultyThread()
35 | {
36 | try
37 | {
38 | WriteLine("Starting a faulty thread...");
39 | Sleep(TimeSpan.FromSeconds(1));
40 | throw new Exception("Boom!");
41 | }
42 | catch (Exception ex)
43 | {
44 | WriteLine($"Exception handled: {ex.Message}");
45 | }
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/Chapter1/Recipe11/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe14")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe14")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("fbf98b90-1596-4fca-bafb-44ce73b4718a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Thread t = new Thread(PrintNumbersWithDelay);
13 | t.Start();
14 | PrintNumbers();
15 | }
16 |
17 | static void PrintNumbers()
18 | {
19 | WriteLine("Starting...");
20 | for (int i = 1; i < 10; i++)
21 | {
22 | WriteLine(i);
23 | }
24 | }
25 |
26 | static void PrintNumbersWithDelay()
27 | {
28 | WriteLine("Starting...");
29 | for (int i = 1; i < 10; i++)
30 | {
31 | Sleep(TimeSpan.FromSeconds(2));
32 | WriteLine(i);
33 | }
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("28b0ed05-0050-4d9f-b814-26dd1d236523")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe3
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Starting program...");
13 | Thread t = new Thread(PrintNumbersWithDelay);
14 | t.Start();
15 | t.Join();
16 | WriteLine("Thread completed");
17 | }
18 |
19 | static void PrintNumbersWithDelay()
20 | {
21 | WriteLine("Starting...");
22 | for (int i = 1; i < 10; i++)
23 | {
24 | Sleep(TimeSpan.FromSeconds(2));
25 | WriteLine(i);
26 | }
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter1/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("56583f3d-119b-4aca-840b-bf2ee53db6fd")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe4
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Starting program...");
13 | Thread t = new Thread(PrintNumbersWithDelay);
14 | t.Start();
15 | Sleep(TimeSpan.FromSeconds(6));
16 | t.Abort();
17 | WriteLine("A thread has been aborted");
18 | }
19 |
20 | static void PrintNumbersWithDelay()
21 | {
22 | WriteLine("Starting...");
23 | for (int i = 1; i < 10; i++)
24 | {
25 | Sleep(TimeSpan.FromSeconds(2));
26 | WriteLine(i);
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter1/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("78b7a1e7-835b-4aec-a556-94d6b76e7080")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe5
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Starting program...");
13 | Thread t = new Thread(PrintNumbersWithStatus);
14 | Thread t2 = new Thread(DoNothing);
15 | WriteLine(t.ThreadState.ToString());
16 | t2.Start();
17 | t.Start();
18 | for (int i = 1; i < 30; i++)
19 | {
20 | WriteLine(t.ThreadState.ToString());
21 | }
22 | Sleep(TimeSpan.FromSeconds(6));
23 | t.Abort();
24 | WriteLine("A thread has been aborted");
25 | WriteLine(t.ThreadState.ToString());
26 | WriteLine(t2.ThreadState.ToString());
27 | }
28 |
29 | static void DoNothing()
30 | {
31 | Sleep(TimeSpan.FromSeconds(2));
32 | }
33 |
34 | static void PrintNumbersWithStatus()
35 | {
36 | WriteLine("Starting...");
37 | WriteLine(CurrentThread.ThreadState.ToString());
38 | for (int i = 1; i < 10; i++)
39 | {
40 | Sleep(TimeSpan.FromSeconds(2));
41 | WriteLine(i);
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/Chapter1/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("3004638c-a1c6-4c9a-ae23-8b1c080d5682")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 | using static System.Diagnostics.Process;
6 |
7 | namespace Chapter1.Recipe6
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | WriteLine($"Current thread priority: {CurrentThread.Priority}");
14 | WriteLine("Running on all cores available");
15 | RunThreads();
16 | Sleep(TimeSpan.FromSeconds(2));
17 | WriteLine("Running on a single core");
18 | GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
19 | RunThreads();
20 | }
21 |
22 | static void RunThreads()
23 | {
24 | var sample = new ThreadSample();
25 |
26 | var threadOne = new Thread(sample.CountNumbers);
27 | threadOne.Name = "ThreadOne";
28 | var threadTwo = new Thread(sample.CountNumbers);
29 | threadTwo.Name = "ThreadTwo";
30 |
31 | threadOne.Priority = ThreadPriority.Highest;
32 | threadTwo.Priority = ThreadPriority.Lowest;
33 | threadOne.Start();
34 | threadTwo.Start();
35 |
36 | Sleep(TimeSpan.FromSeconds(2));
37 | sample.Stop();
38 | }
39 |
40 | class ThreadSample
41 | {
42 | private bool _isStopped = false;
43 |
44 | public void Stop()
45 | {
46 | _isStopped = true;
47 | }
48 |
49 | public void CountNumbers()
50 | {
51 | long counter = 0;
52 |
53 | while (!_isStopped)
54 | {
55 | counter++;
56 | }
57 |
58 | WriteLine($"{CurrentThread.Name} with " +
59 | $"{CurrentThread.Priority,11} priority " +
60 | $"has a count = {counter,13:N0}");
61 | }
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Chapter1/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("2b30c7ae-5ce3-49bb-9cdf-d2516d2290be")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe7
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var sampleForeground = new ThreadSample(10);
13 | var sampleBackground = new ThreadSample(20);
14 |
15 | var threadOne = new Thread(sampleForeground.CountNumbers);
16 | threadOne.Name = "ForegroundThread";
17 | var threadTwo = new Thread(sampleBackground.CountNumbers);
18 | threadTwo.Name = "BackgroundThread";
19 | threadTwo.IsBackground = true;
20 |
21 | threadOne.Start();
22 | threadTwo.Start();
23 | }
24 |
25 | class ThreadSample
26 | {
27 | private readonly int _iterations;
28 |
29 | public ThreadSample(int iterations)
30 | {
31 | _iterations = iterations;
32 | }
33 | public void CountNumbers()
34 | {
35 | for (int i = 0; i < _iterations; i++)
36 | {
37 | Sleep(TimeSpan.FromSeconds(0.5));
38 | WriteLine($"{CurrentThread.Name} prints {i}");
39 | }
40 | }
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter1/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("49a9121c-996a-4eb5-a0e8-e86475e0dd16")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter1.Recipe8
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var sample = new ThreadSample(10);
13 |
14 | var threadOne = new Thread(sample.CountNumbers);
15 | threadOne.Name = "ThreadOne";
16 | threadOne.Start();
17 | threadOne.Join();
18 |
19 | WriteLine("--------------------------");
20 |
21 | var threadTwo = new Thread(Count);
22 | threadTwo.Name = "ThreadTwo";
23 | threadTwo.Start(8);
24 | threadTwo.Join();
25 |
26 | WriteLine("--------------------------");
27 |
28 | var threadThree = new Thread(() => CountNumbers(12));
29 | threadThree.Name = "ThreadThree";
30 | threadThree.Start();
31 | threadThree.Join();
32 | WriteLine("--------------------------");
33 |
34 | int i = 10;
35 | var threadFour = new Thread(() => PrintNumber(i));
36 | i = 20;
37 | var threadFive = new Thread(() => PrintNumber(i));
38 | threadFour.Start();
39 | threadFive.Start();
40 | }
41 |
42 | static void Count(object iterations)
43 | {
44 | CountNumbers((int)iterations);
45 | }
46 |
47 | static void CountNumbers(int iterations)
48 | {
49 | for (int i = 1; i <= iterations; i++)
50 | {
51 | Sleep(TimeSpan.FromSeconds(0.5));
52 | WriteLine($"{CurrentThread.Name} prints {i}");
53 | }
54 | }
55 |
56 | static void PrintNumber(int number)
57 | {
58 | WriteLine(number);
59 | }
60 |
61 | class ThreadSample
62 | {
63 | private readonly int _iterations;
64 |
65 | public ThreadSample(int iterations)
66 | {
67 | _iterations = iterations;
68 | }
69 | public void CountNumbers()
70 | {
71 | for (int i = 1; i <= _iterations; i++)
72 | {
73 | Sleep(TimeSpan.FromSeconds(0.5));
74 | WriteLine($"{CurrentThread.Name} prints {i}");
75 | }
76 | }
77 | }
78 | }
79 |
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/Chapter1/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("2cbac03d-c6da-4d55-98cd-9bdfa2e11564")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter1/Recipe9/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 |
5 | namespace Chapter1.Recipe9
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | WriteLine("Incorrect counter");
12 |
13 | var c = new Counter();
14 |
15 | var t1 = new Thread(() => TestCounter(c));
16 | var t2 = new Thread(() => TestCounter(c));
17 | var t3 = new Thread(() => TestCounter(c));
18 | t1.Start();
19 | t2.Start();
20 | t3.Start();
21 | t1.Join();
22 | t2.Join();
23 | t3.Join();
24 |
25 | WriteLine($"Total count: {c.Count}");
26 | WriteLine("--------------------------");
27 |
28 | WriteLine("Correct counter");
29 |
30 | var c1 = new CounterWithLock();
31 |
32 | t1 = new Thread(() => TestCounter(c1));
33 | t2 = new Thread(() => TestCounter(c1));
34 | t3 = new Thread(() => TestCounter(c1));
35 | t1.Start();
36 | t2.Start();
37 | t3.Start();
38 | t1.Join();
39 | t2.Join();
40 | t3.Join();
41 | WriteLine($"Total count: {c1.Count}");
42 | }
43 |
44 | static void TestCounter(CounterBase c)
45 | {
46 | for (int i = 0; i < 100000; i++)
47 | {
48 | c.Increment();
49 | c.Decrement();
50 | }
51 | }
52 |
53 | class Counter : CounterBase
54 | {
55 | public int Count { get; private set; }
56 |
57 | public override void Increment()
58 | {
59 | Count++;
60 | }
61 |
62 | public override void Decrement()
63 | {
64 | Count--;
65 | }
66 | }
67 |
68 | class CounterWithLock : CounterBase
69 | {
70 | private readonly object _syncRoot = new Object();
71 |
72 | public int Count { get; private set; }
73 |
74 | public override void Increment()
75 | {
76 | lock (_syncRoot)
77 | {
78 | Count++;
79 | }
80 | }
81 |
82 | public override void Decrement()
83 | {
84 | lock (_syncRoot)
85 | {
86 | Count--;
87 | }
88 | }
89 | }
90 |
91 | abstract class CounterBase
92 | {
93 | public abstract void Increment();
94 |
95 | public abstract void Decrement();
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/Chapter1/Recipe9/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe9")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe9")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("a07629bb-ca3f-42d6-8ea6-c99bf31637a8")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("52b0193d-762f-4d4f-a8fe-7c86237d1270")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("be99ea07-302f-490c-a775-f1706d4d10c0")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f399b1fb-7f3f-4bbc-807a-9ab99f4f1d1f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe3/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter10/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("63702efc-ff1e-43cb-97d4-6ae930b2feb7")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter10/Recipe4/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/App.xaml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/ApplicationInsights.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/LockScreenLogo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/LockScreenLogo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/SplashScreen.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/SplashScreen.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/Square150x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square150x150Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/Square44x44Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square44x44Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/Square44x44Logo.targetsize-24_altform-unplated.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Square44x44Logo.targetsize-24_altform-unplated.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/StoreLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/StoreLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Assets/Wide310x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Assets/Wide310x150Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe1/MainPage.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Package.appxmanifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
13 |
14 |
15 |
16 |
17 | Recipe1
18 | vagrant
19 | Assets\StoreLogo.png
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
34 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Vagrant")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © Vagrant 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Version information for an assembly consists of the following four values:
18 | //
19 | // Major Version
20 | // Minor Version
21 | // Build Number
22 | // Revision
23 | //
24 | // You can specify all the values or you can default the Build and Revision Numbers
25 | // by using the '*' as shown below:
26 | // [assembly: AssemblyVersion("1.0.*")]
27 | [assembly: AssemblyVersion("1.0.0.0")]
28 | [assembly: AssemblyFileVersion("1.0.0.0")]
29 | [assembly: ComVisible(false)]
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Properties/Default.rd.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Chapter11/Recipe1/Recipe1_TemporaryKey.pfx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe1/Recipe1_TemporaryKey.pfx
--------------------------------------------------------------------------------
/Chapter11/Recipe1/project.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "Microsoft.ApplicationInsights": "1.0.0",
4 | "Microsoft.ApplicationInsights.PersistenceChannel": "1.0.0",
5 | "Microsoft.ApplicationInsights.WindowsApps": "1.0.0",
6 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
7 | },
8 | "frameworks": {
9 | "uap10.0": {}
10 | },
11 | "runtimes": {
12 | "win10-arm": {},
13 | "win10-arm-aot": {},
14 | "win10-x86": {},
15 | "win10-x86-aot": {},
16 | "win10-x64": {},
17 | "win10-x64-aot": {}
18 | }
19 | }
--------------------------------------------------------------------------------
/Chapter11/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Threading.Tasks;
4 | using Windows.Storage;
5 |
6 | namespace Chapter11.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t = AsynchronousProcessing();
13 | t.GetAwaiter().GetResult();
14 | Console.WriteLine();
15 | Console.WriteLine("Press ENTER to continue");
16 | Console.ReadLine();
17 | }
18 |
19 | async static Task AsynchronousProcessing()
20 | {
21 | StorageFolder folder = KnownFolders.DocumentsLibrary;
22 |
23 | if (await folder.DoesFileExistAsync("test.txt"))
24 | {
25 | var fileToDelete = await folder.GetFileAsync("test.txt");
26 | await fileToDelete.DeleteAsync(StorageDeleteOption.PermanentDelete);
27 | }
28 |
29 | var file = await folder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
30 | using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
31 | using (var writer = new StreamWriter(stream.AsStreamForWrite()))
32 | {
33 | await writer.WriteLineAsync("Test content");
34 | await writer.FlushAsync();
35 | }
36 |
37 | using (var stream = await file.OpenAsync(FileAccessMode.Read))
38 | using (var reader = new StreamReader(stream.AsStreamForRead()))
39 | {
40 | string content = await reader.ReadToEndAsync();
41 | Console.WriteLine(content);
42 | }
43 |
44 | Console.WriteLine("Enumerating Folder Structure:");
45 |
46 | var itemsList = await folder.GetItemsAsync();
47 | foreach (var item in itemsList)
48 | {
49 | if (item is StorageFolder)
50 | {
51 | Console.WriteLine("{0} folder", item.Name);
52 | }
53 | else
54 | {
55 | Console.WriteLine(item.Name);
56 | }
57 | }
58 | }
59 | }
60 |
61 | static class Extensions
62 | {
63 | public static async Task DoesFileExistAsync(this StorageFolder folder, string fileName)
64 | {
65 | try
66 | {
67 | await folder.GetFileAsync(fileName);
68 | return true;
69 | }
70 | catch (FileNotFoundException)
71 | {
72 | return false;
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/Chapter11/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("e6053ad8-4fb1-42ff-8b68-55932e053dfc")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/App.xaml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/ApplicationInsights.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/LockScreenLogo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/LockScreenLogo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/SplashScreen.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/SplashScreen.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/Square150x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square150x150Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/Square44x44Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square44x44Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/Square44x44Logo.targetsize-24_altform-unplated.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Square44x44Logo.targetsize-24_altform-unplated.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/StoreLogo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/StoreLogo.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Assets/Wide310x150Logo.scale-200.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Assets/Wide310x150Logo.scale-200.png
--------------------------------------------------------------------------------
/Chapter11/Recipe3/MainPage.xaml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Package.appxmanifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Recipe3
7 | vagrant
8 | Assets\StoreLogo.png
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Vagrant")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © Vagrant 2016")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Version information for an assembly consists of the following four values:
18 | //
19 | // Major Version
20 | // Minor Version
21 | // Build Number
22 | // Revision
23 | //
24 | // You can specify all the values or you can default the Build and Revision Numbers
25 | // by using the '*' as shown below:
26 | // [assembly: AssemblyVersion("1.0.*")]
27 | [assembly: AssemblyVersion("1.0.0.0")]
28 | [assembly: AssemblyFileVersion("1.0.0.0")]
29 | [assembly: ComVisible(false)]
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Properties/Default.rd.xml:
--------------------------------------------------------------------------------
1 |
17 |
18 |
19 |
20 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Chapter11/Recipe3/Recipe3_TemporaryKey.pfx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/MultithreadingwithCSharpCookbookSecondEdition_Code/3dca2ca64f6d7a0380721cc7d947d4e19625214a/Chapter11/Recipe3/Recipe3_TemporaryKey.pfx
--------------------------------------------------------------------------------
/Chapter11/Recipe3/project.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | "Microsoft.ApplicationInsights": "1.0.0",
4 | "Microsoft.ApplicationInsights.PersistenceChannel": "1.0.0",
5 | "Microsoft.ApplicationInsights.WindowsApps": "1.0.0",
6 | "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
7 | },
8 | "frameworks": {
9 | "uap10.0": {}
10 | },
11 | "runtimes": {
12 | "win10-arm": {},
13 | "win10-arm-aot": {},
14 | "win10-x86": {},
15 | "win10-x86-aot": {},
16 | "win10-x64": {},
17 | "win10-x64-aot": {}
18 | }
19 | }
--------------------------------------------------------------------------------
/Chapter11/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 |
5 | namespace OSXConsoleApplication
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | WriteLine(".NET Core app on OS X");
12 | RunCodeAsync().GetAwaiter().GetResult();
13 | }
14 |
15 | static async Task RunCodeAsync()
16 | {
17 | try
18 | {
19 | string result = await GetInfoAsync("Async 1");
20 | WriteLine(result);
21 | result = await GetInfoAsync("Async 2");
22 | WriteLine(result);
23 | }
24 | catch (Exception ex)
25 | {
26 | WriteLine(ex);
27 | }
28 | }
29 |
30 | static async Task GetInfoAsync(string name)
31 | {
32 | WriteLine($"Task {name} started!");
33 | await Task.Delay(TimeSpan.FromSeconds(2));
34 | if(name == "Async 2")
35 | throw new Exception("Boom!");
36 | return
37 | $"Task {name} completed successfully!"
38 | // + $"Thread id {System.Threading.Thread.CurrentThread.ManagedThreadId}."
39 | ;
40 | }
41 | }
42 | }
--------------------------------------------------------------------------------
/Chapter11/Recipe4/dotnetcore_installation.txt:
--------------------------------------------------------------------------------
1 | #please do it on your own risk!
2 | sudo sysctl -w kern.maxfiles=20480
3 |
4 | sudo sysctl -w kern.maxfilesperproc=18000
5 |
6 | sudo ulimit -S -n 2048
7 |
8 | mkdir HelloWorld
9 |
10 | cd HelloWorld
11 |
12 | dotnet new
13 |
14 | dotnet restore
15 |
16 | dotnet run
17 |
--------------------------------------------------------------------------------
/Chapter11/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 |
5 | namespace OSXConsoleApplication
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | WriteLine(".NET Core app on Ubuntu");
12 | RunCodeAsync().GetAwaiter().GetResult();
13 | }
14 |
15 | static async Task RunCodeAsync()
16 | {
17 | try
18 | {
19 | string result = await GetInfoAsync("Async 1");
20 | WriteLine(result);
21 | result = await GetInfoAsync("Async 2");
22 | WriteLine(result);
23 | }
24 | catch (Exception ex)
25 | {
26 | WriteLine(ex);
27 | }
28 | }
29 |
30 | static async Task GetInfoAsync(string name)
31 | {
32 | WriteLine($"Task {name} started!");
33 | await Task.Delay(TimeSpan.FromSeconds(2));
34 | if(name == "Async 2")
35 | throw new Exception("Boom!");
36 | return
37 | $"Task {name} completed successfully!"
38 | // + $"Thread id {System.Threading.Thread.CurrentThread.ManagedThreadId}."
39 | ;
40 | }
41 | }
42 | }
--------------------------------------------------------------------------------
/Chapter11/Recipe5/dotnetcore_installation.txt:
--------------------------------------------------------------------------------
1 | sudo sh -c 'echo "deb [arch=amd64] http://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
2 |
3 | sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893
4 |
5 | sudo apt-get update
6 |
7 | mkdir HelloWorld
8 |
9 | cd HelloWorld
10 |
11 | dotnet new
12 |
13 | dotnet restore
14 |
15 | dotnet run
--------------------------------------------------------------------------------
/Chapter2/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 |
5 | namespace Chapter2.Recipe1
6 | {
7 | internal class Program
8 | {
9 | private static void Main(string[] args)
10 | {
11 | WriteLine("Incorrect counter");
12 |
13 | var c = new Counter();
14 |
15 | var t1 = new Thread(() => TestCounter(c));
16 | var t2 = new Thread(() => TestCounter(c));
17 | var t3 = new Thread(() => TestCounter(c));
18 | t1.Start();
19 | t2.Start();
20 | t3.Start();
21 | t1.Join();
22 | t2.Join();
23 | t3.Join();
24 |
25 | WriteLine($"Total count: {c.Count}");
26 | WriteLine("--------------------------");
27 |
28 | WriteLine("Correct counter");
29 |
30 | var c1 = new CounterNoLock();
31 |
32 | t1 = new Thread(() => TestCounter(c1));
33 | t2 = new Thread(() => TestCounter(c1));
34 | t3 = new Thread(() => TestCounter(c1));
35 | t1.Start();
36 | t2.Start();
37 | t3.Start();
38 | t1.Join();
39 | t2.Join();
40 | t3.Join();
41 |
42 | WriteLine($"Total count: {c1.Count}");
43 | }
44 |
45 | static void TestCounter(CounterBase c)
46 | {
47 | for (int i = 0; i < 100000; i++)
48 | {
49 | c.Increment();
50 | c.Decrement();
51 | }
52 | }
53 |
54 | class Counter : CounterBase
55 | {
56 | private int _count;
57 |
58 | public int Count => _count;
59 |
60 | public override void Increment()
61 | {
62 | _count++;
63 | }
64 |
65 | public override void Decrement()
66 | {
67 | _count--;
68 | }
69 | }
70 |
71 | class CounterNoLock : CounterBase
72 | {
73 | private int _count;
74 |
75 | public int Count => _count;
76 |
77 | public override void Increment()
78 | {
79 | Interlocked.Increment(ref _count);
80 | }
81 |
82 | public override void Decrement()
83 | {
84 | Interlocked.Decrement(ref _count);
85 | }
86 | }
87 |
88 | abstract class CounterBase
89 | {
90 | public abstract void Increment();
91 |
92 | public abstract void Decrement();
93 | }
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/Chapter2/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("944a1eb5-90f6-4365-98a0-b45118f56175")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 |
5 | namespace Chapter2.Recipe2
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | const string MutexName = "CSharpThreadingCookbook";
12 |
13 | using (var m = new Mutex(false, MutexName))
14 | {
15 | if (!m.WaitOne(TimeSpan.FromSeconds(5), false))
16 | {
17 | WriteLine("Second instance is running!");
18 | }
19 | else
20 | {
21 | WriteLine("Running!");
22 | ReadLine();
23 | m.ReleaseMutex();
24 | }
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter2/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c63b4870-e7e6-42b1-aba2-f0f6b24fa94f")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe3
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | for (int i = 1; i <= 6; i++)
13 | {
14 | string threadName = "Thread " + i;
15 | int secondsToWait = 2 + 2 * i;
16 | var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
17 | t.Start();
18 | }
19 | }
20 |
21 | static SemaphoreSlim _semaphore = new SemaphoreSlim(4);
22 |
23 | static void AccessDatabase(string name, int seconds)
24 | {
25 | WriteLine($"{name} waits to access a database");
26 | _semaphore.Wait();
27 | WriteLine($"{name} was granted an access to a database");
28 | Sleep(TimeSpan.FromSeconds(seconds));
29 | WriteLine($"{name} is completed");
30 | _semaphore.Release();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter2/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7fcc52db-8509-4f1a-9d02-7921a430505d")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe4/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe4
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t = new Thread(() => Process(10));
13 | t.Start();
14 |
15 | WriteLine("Waiting for another thread to complete work");
16 | _workerEvent.WaitOne();
17 | WriteLine("First operation is completed!");
18 | WriteLine("Performing an operation on a main thread");
19 | Sleep(TimeSpan.FromSeconds(5));
20 | _mainEvent.Set();
21 | WriteLine("Now running the second operation on a second thread");
22 | _workerEvent.WaitOne();
23 | WriteLine("Second operation is completed!");
24 | }
25 |
26 | private static AutoResetEvent _workerEvent = new AutoResetEvent(false);
27 | private static AutoResetEvent _mainEvent = new AutoResetEvent(false);
28 |
29 | static void Process(int seconds)
30 | {
31 | WriteLine("Starting a long running work...");
32 | Sleep(TimeSpan.FromSeconds(seconds));
33 | WriteLine("Work is done!");
34 | _workerEvent.Set();
35 | WriteLine("Waiting for a main thread to complete its work");
36 | _mainEvent.WaitOne();
37 | WriteLine("Starting second operation...");
38 | Sleep(TimeSpan.FromSeconds(seconds));
39 | WriteLine("Work is done!");
40 | _workerEvent.Set();
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter2/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8bad7962-39eb-4047-a3b7-024fda286eec")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe5
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t1 = new Thread(() => TravelThroughGates("Thread 1", 5));
13 | var t2 = new Thread(() => TravelThroughGates("Thread 2", 6));
14 | var t3 = new Thread(() => TravelThroughGates("Thread 3", 12));
15 | t1.Start();
16 | t2.Start();
17 | t3.Start();
18 | Sleep(TimeSpan.FromSeconds(6));
19 | WriteLine("The gates are now open!");
20 | _mainEvent.Set();
21 | Sleep(TimeSpan.FromSeconds(2));
22 | _mainEvent.Reset();
23 | WriteLine("The gates have been closed!");
24 | Sleep(TimeSpan.FromSeconds(10));
25 | WriteLine("The gates are now open for the second time!");
26 | _mainEvent.Set();
27 | Sleep(TimeSpan.FromSeconds(2));
28 | WriteLine("The gates have been closed!");
29 | _mainEvent.Reset();
30 | }
31 |
32 | static void TravelThroughGates(string threadName, int seconds)
33 | {
34 | WriteLine($"{threadName} falls to sleep");
35 | Sleep(TimeSpan.FromSeconds(seconds));
36 | WriteLine($"{threadName} waits for the gates to open!");
37 | _mainEvent.Wait();
38 | WriteLine($"{threadName} enters the gates!");
39 | }
40 |
41 | static ManualResetEventSlim _mainEvent = new ManualResetEventSlim(false);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter2/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("793034d9-1c36-4858-a4cc-7182286e0ff8")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe6
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Starting two operations");
13 | var t1 = new Thread(() => PerformOperation("Operation 1 is completed", 4));
14 | var t2 = new Thread(() => PerformOperation("Operation 2 is completed", 8));
15 | t1.Start();
16 | t2.Start();
17 | _countdown.Wait();
18 | WriteLine("Both operations have been completed.");
19 | _countdown.Dispose();
20 | }
21 |
22 | static CountdownEvent _countdown = new CountdownEvent(2);
23 |
24 | static void PerformOperation(string message, int seconds)
25 | {
26 | Sleep(TimeSpan.FromSeconds(seconds));
27 | WriteLine(message);
28 | _countdown.Signal();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter2/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7732c65d-fdd1-4322-ad33-2ab587390a3e")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe7
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t1 = new Thread(() => PlayMusic("the guitarist", "play an amazing solo", 5));
13 | var t2 = new Thread(() => PlayMusic("the singer", "sing his song", 2));
14 |
15 | t1.Start();
16 | t2.Start();
17 | }
18 |
19 | static Barrier _barrier = new Barrier(2,
20 | b => WriteLine($"End of phase {b.CurrentPhaseNumber + 1}"));
21 |
22 | static void PlayMusic(string name, string message, int seconds)
23 | {
24 | for (int i = 1; i < 3; i++)
25 | {
26 | WriteLine("----------------------------------------------");
27 | Sleep(TimeSpan.FromSeconds(seconds));
28 | WriteLine($"{name} starts to {message}");
29 | Sleep(TimeSpan.FromSeconds(seconds));
30 | WriteLine($"{name} finishes to {message}");
31 | _barrier.SignalAndWait();
32 | }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/Chapter2/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c8637971-a29b-4354-8a16-c812cd0af604")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading;
4 | using static System.Console;
5 | using static System.Threading.Thread;
6 |
7 | namespace Chapter2.Recipe8
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | new Thread(Read){ IsBackground = true }.Start();
14 | new Thread(Read){ IsBackground = true }.Start();
15 | new Thread(Read){ IsBackground = true }.Start();
16 |
17 | new Thread(() => Write("Thread 1")){ IsBackground = true }.Start();
18 | new Thread(() => Write("Thread 2")){ IsBackground = true }.Start();
19 |
20 | Sleep(TimeSpan.FromSeconds(30));
21 | }
22 |
23 | static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim();
24 | static Dictionary _items = new Dictionary();
25 |
26 | static void Read()
27 | {
28 | WriteLine("Reading contents of a dictionary");
29 | while (true)
30 | {
31 | try
32 | {
33 | _rw.EnterReadLock();
34 | foreach (var key in _items.Keys)
35 | {
36 | Sleep(TimeSpan.FromSeconds(0.1));
37 | }
38 | }
39 | finally
40 | {
41 | _rw.ExitReadLock();
42 | }
43 | }
44 | }
45 |
46 | static void Write(string threadName)
47 | {
48 | while (true)
49 | {
50 | try
51 | {
52 | int newKey = new Random().Next(250);
53 | _rw.EnterUpgradeableReadLock();
54 | if (!_items.ContainsKey(newKey))
55 | {
56 | try
57 | {
58 | _rw.EnterWriteLock();
59 | _items[newKey] = 1;
60 | WriteLine($"New key {newKey} is added to a dictionary by a {threadName}");
61 | }
62 | finally
63 | {
64 | _rw.ExitWriteLock();
65 | }
66 | }
67 | Sleep(TimeSpan.FromSeconds(0.1));
68 | }
69 | finally
70 | {
71 | _rw.ExitUpgradeableReadLock();
72 | }
73 | }
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/Chapter2/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("4489f834-2752-456f-8baf-103d84cf6841")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter2/Recipe9/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter2.Recipe9
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t1 = new Thread(UserModeWait);
13 | var t2 = new Thread(HybridSpinWait);
14 |
15 | WriteLine("Running user mode waiting");
16 | t1.Start();
17 | Sleep(20);
18 | _isCompleted = true;
19 | Sleep(TimeSpan.FromSeconds(1));
20 | _isCompleted = false;
21 | WriteLine("Running hybrid SpinWait construct waiting");
22 | t2.Start();
23 | Sleep(5);
24 | _isCompleted = true;
25 | }
26 |
27 | static volatile bool _isCompleted = false;
28 |
29 | static void UserModeWait()
30 | {
31 | while (!_isCompleted)
32 | {
33 | Write(".");
34 | }
35 | WriteLine();
36 | WriteLine("Waiting is complete");
37 | }
38 |
39 | static void HybridSpinWait()
40 | {
41 | var w = new SpinWait();
42 | while (!_isCompleted)
43 | {
44 | w.SpinOnce();
45 | WriteLine(w.NextSpinWillYield);
46 | }
47 | WriteLine("Waiting is complete");
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter2/Recipe9/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe9")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe9")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7bdbe352-3307-41db-b076-3a7d4727a1d5")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter3.Recipe1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | int threadId = 0;
13 |
14 | RunOnThreadPool poolDelegate = Test;
15 |
16 | var t = new Thread(() => Test(out threadId));
17 | t.Start();
18 | t.Join();
19 |
20 | WriteLine($"Thread id: {threadId}");
21 |
22 | IAsyncResult r = poolDelegate.BeginInvoke(out threadId, Callback, "a delegate asynchronous call");
23 | r.AsyncWaitHandle.WaitOne();
24 |
25 | string result = poolDelegate.EndInvoke(out threadId, r);
26 |
27 | WriteLine($"Thread pool worker thread id: {threadId}");
28 | WriteLine(result);
29 |
30 | Sleep(TimeSpan.FromSeconds(2));
31 | }
32 |
33 | private delegate string RunOnThreadPool(out int threadId);
34 |
35 | private static void Callback(IAsyncResult ar)
36 | {
37 | WriteLine("Starting a callback...");
38 | WriteLine($"State passed to a callbak: {ar.AsyncState}");
39 | WriteLine($"Is thread pool thread: {CurrentThread.IsThreadPoolThread}");
40 | WriteLine($"Thread pool worker thread id: {CurrentThread.ManagedThreadId}");
41 | }
42 |
43 |
44 | private static string Test(out int threadId)
45 | {
46 | WriteLine("Starting...");
47 | WriteLine($"Is thread pool thread: {CurrentThread.IsThreadPoolThread}");
48 | Sleep(TimeSpan.FromSeconds(2));
49 | threadId = CurrentThread.ManagedThreadId;
50 | return $"Thread pool worker thread id was: {threadId}";
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/Chapter3/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f6db0a1e-2c72-496a-83fa-f3d9e60a4038")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter3.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | const int x = 1;
13 | const int y = 2;
14 | const string lambdaState = "lambda state 2";
15 |
16 | ThreadPool.QueueUserWorkItem(AsyncOperation);
17 | Sleep(TimeSpan.FromSeconds(1));
18 |
19 | ThreadPool.QueueUserWorkItem(AsyncOperation, "async state");
20 | Sleep(TimeSpan.FromSeconds(1));
21 |
22 | ThreadPool.QueueUserWorkItem( state =>
23 | {
24 | WriteLine($"Operation state: {state}");
25 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}");
26 | Sleep(TimeSpan.FromSeconds(2));
27 | }, "lambda state");
28 |
29 | ThreadPool.QueueUserWorkItem( _ =>
30 | {
31 | WriteLine($"Operation state: {x + y}, {lambdaState}");
32 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}");
33 | Sleep(TimeSpan.FromSeconds(2));
34 | }, "lambda state");
35 |
36 | Sleep(TimeSpan.FromSeconds(2));
37 | }
38 |
39 | private static void AsyncOperation(object state)
40 | {
41 | WriteLine($"Operation state: {state ?? "(null)"}");
42 | WriteLine($"Worker thread id: {CurrentThread.ManagedThreadId}");
43 | Sleep(TimeSpan.FromSeconds(2));
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Chapter3/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("98411a29-94b5-4fe5-84aa-4be05008e3ab")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe3/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Threading;
4 | using static System.Console;
5 | using static System.Threading.Thread;
6 |
7 | namespace Chapter3.Recipe3
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | const int numberOfOperations = 500;
14 | var sw = new Stopwatch();
15 | sw.Start();
16 | UseThreads(numberOfOperations);
17 | sw.Stop();
18 | WriteLine($"Execution time using threads: {sw.ElapsedMilliseconds}");
19 |
20 | sw.Reset();
21 | sw.Start();
22 | UseThreadPool(numberOfOperations);
23 | sw.Stop();
24 | WriteLine($"Execution time using the thread pool: {sw.ElapsedMilliseconds}");
25 | }
26 |
27 | static void UseThreads(int numberOfOperations)
28 | {
29 | using (var countdown = new CountdownEvent(numberOfOperations))
30 | {
31 | WriteLine("Scheduling work by creating threads");
32 | for (int i = 0; i < numberOfOperations; i++)
33 | {
34 | var thread = new Thread(() =>
35 | {
36 | Write($"{CurrentThread.ManagedThreadId},");
37 | Sleep(TimeSpan.FromSeconds(0.1));
38 | countdown.Signal();
39 | });
40 | thread.Start();
41 | }
42 | countdown.Wait();
43 | WriteLine();
44 | }
45 | }
46 |
47 | static void UseThreadPool(int numberOfOperations)
48 | {
49 | using (var countdown = new CountdownEvent(numberOfOperations))
50 | {
51 | WriteLine("Starting work on a threadpool");
52 | for (int i = 0; i < numberOfOperations; i++)
53 | {
54 | ThreadPool.QueueUserWorkItem( _ =>
55 | {
56 | Write($"{CurrentThread.ManagedThreadId},");
57 | Sleep(TimeSpan.FromSeconds(0.1));
58 | countdown.Signal();
59 | });
60 | }
61 | countdown.Wait();
62 | WriteLine();
63 | }
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/Chapter3/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("59d0805c-d2ea-451a-9e73-5ca0f55d3189")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("70cc3a8a-9ecb-4c51-ad54-516cbda89010")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 |
7 | namespace Chapter3.Recipe5
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | RunOperations(TimeSpan.FromSeconds(5));
14 | RunOperations(TimeSpan.FromSeconds(7));
15 | }
16 |
17 | static void RunOperations(TimeSpan workerOperationTimeout)
18 | {
19 | using (var evt = new ManualResetEvent(false))
20 | using (var cts = new CancellationTokenSource())
21 | {
22 | WriteLine("Registering timeout operation...");
23 | var worker = ThreadPool.RegisterWaitForSingleObject(evt
24 | , (state, isTimedOut) => WorkerOperationWait(cts, isTimedOut)
25 | , null
26 | , workerOperationTimeout
27 | , true);
28 |
29 | WriteLine("Starting long running operation...");
30 | ThreadPool.QueueUserWorkItem(_ => WorkerOperation(cts.Token, evt));
31 |
32 | Sleep(workerOperationTimeout.Add(TimeSpan.FromSeconds(2)));
33 | worker.Unregister(evt);
34 | }
35 | }
36 |
37 | static void WorkerOperation(CancellationToken token, ManualResetEvent evt)
38 | {
39 | for(int i = 0; i < 6; i++)
40 | {
41 | if (token.IsCancellationRequested)
42 | {
43 | return;
44 | }
45 | Sleep(TimeSpan.FromSeconds(1));
46 | }
47 | evt.Set();
48 | }
49 |
50 | static void WorkerOperationWait(CancellationTokenSource cts, bool isTimedOut)
51 | {
52 | if (isTimedOut)
53 | {
54 | cts.Cancel();
55 | WriteLine("Worker operation timed out and was canceled.");
56 | }
57 | else
58 | {
59 | WriteLine("Worker operation succeded.");
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/Chapter3/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c2191460-ac35-448f-9f04-0c8ca5753006")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter3.Recipe6
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Press 'Enter' to stop the timer...");
13 | DateTime start = DateTime.Now;
14 | _timer = new Timer(_ => TimerOperation(start), null
15 | , TimeSpan.FromSeconds(1)
16 | , TimeSpan.FromSeconds(2));
17 | try
18 | {
19 | Sleep(TimeSpan.FromSeconds(6));
20 |
21 | _timer.Change(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(4));
22 |
23 | ReadLine();
24 | }
25 | finally
26 | {
27 | _timer.Dispose();
28 | }
29 | }
30 |
31 | static Timer _timer;
32 |
33 | static void TimerOperation(DateTime start)
34 | {
35 | TimeSpan elapsed = DateTime.Now - start;
36 | WriteLine($"{elapsed.Seconds} seconds from {start}. " +
37 | $"Timer thread pool thread id: {CurrentThread.ManagedThreadId}");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/Chapter3/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("7fb66af4-43a4-4632-8535-0fa47a07d3d1")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter3/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter3.Recipe7
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var bw = new BackgroundWorker();
13 | bw.WorkerReportsProgress = true;
14 | bw.WorkerSupportsCancellation = true;
15 |
16 | bw.DoWork += Worker_DoWork;
17 | bw.ProgressChanged += Worker_ProgressChanged;
18 | bw.RunWorkerCompleted += Worker_Completed;
19 |
20 | bw.RunWorkerAsync();
21 |
22 | WriteLine("Press C to cancel work");
23 | do
24 | {
25 | if (ReadKey(true).KeyChar == 'C')
26 | {
27 | bw.CancelAsync();
28 | }
29 |
30 | }
31 | while(bw.IsBusy);
32 | }
33 |
34 | static void Worker_DoWork(object sender, DoWorkEventArgs e)
35 | {
36 | WriteLine($"DoWork thread pool thread id: {CurrentThread.ManagedThreadId}");
37 | var bw = (BackgroundWorker) sender;
38 | for (int i = 1; i <= 100; i++)
39 | {
40 | if (bw.CancellationPending)
41 | {
42 | e.Cancel = true;
43 | return;
44 | }
45 | if (i%10 == 0)
46 | {
47 | bw.ReportProgress(i);
48 | }
49 |
50 | Sleep(TimeSpan.FromSeconds(0.1));
51 | }
52 |
53 | e.Result = 42;
54 | }
55 |
56 | static void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
57 | {
58 | WriteLine($"{e.ProgressPercentage}% completed. " +
59 | $"Progress thread pool thread id: {CurrentThread.ManagedThreadId}");
60 | }
61 |
62 | static void Worker_Completed(object sender, RunWorkerCompletedEventArgs e)
63 | {
64 | WriteLine($"Completed thread pool thread id: {CurrentThread.ManagedThreadId}");
65 | if (e.Error != null)
66 | {
67 | WriteLine($"Exception {e.Error.Message} has occured.");
68 | }
69 | else if (e.Cancelled)
70 | {
71 | WriteLine($"Operation has been canceled.");
72 | }
73 | else
74 | {
75 | WriteLine($"The answer is: {e.Result}");
76 | }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/Chapter3/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("d8017f66-6f29-462c-8f8e-59148d9aa447")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe1/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter4.Recipe1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | var t1 = new Task(() => TaskMethod("Task 1"));
13 | var t2 = new Task(() => TaskMethod("Task 2"));
14 | t2.Start();
15 | t1.Start();
16 | Task.Run(() => TaskMethod("Task 3"));
17 | Task.Factory.StartNew(() => TaskMethod("Task 4"));
18 | Task.Factory.StartNew(() => TaskMethod("Task 5"), TaskCreationOptions.LongRunning);
19 | Sleep(TimeSpan.FromSeconds(1));
20 | }
21 |
22 | static void TaskMethod(string name)
23 | {
24 | WriteLine($"Task {name} is running on a thread id " +
25 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
26 | $"{CurrentThread.IsThreadPoolThread}");
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/Chapter4/Recipe1/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe1")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe1")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("540b87a9-d995-4f22-b1d8-e72ffd74a205")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe2/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter4.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | TaskMethod("Main Thread Task");
13 | Task task = CreateTask("Task 1");
14 | task.Start();
15 | int result = task.Result;
16 | WriteLine($"Result is: {result}");
17 |
18 | task = CreateTask("Task 2");
19 | task.RunSynchronously();
20 | result = task.Result;
21 | WriteLine($"Result is: {result}");
22 |
23 | task = CreateTask("Task 3");
24 | WriteLine(task.Status);
25 | task.Start();
26 |
27 | while (!task.IsCompleted)
28 | {
29 | WriteLine(task.Status);
30 | Sleep(TimeSpan.FromSeconds(0.5));
31 | }
32 |
33 | WriteLine(task.Status);
34 | result = task.Result;
35 | WriteLine($"Result is: {result}");
36 | }
37 |
38 | static Task CreateTask(string name)
39 | {
40 | return new Task(() => TaskMethod(name));
41 | }
42 |
43 | static int TaskMethod(string name)
44 | {
45 | WriteLine($"Task {name} is running on a thread id " +
46 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
47 | $"{CurrentThread.IsThreadPoolThread}");
48 | Sleep(TimeSpan.FromSeconds(2));
49 | return 42;
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter4/Recipe2/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe2")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe2")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("db157e8a-39d7-45b8-9938-a9087f6d4a1a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe3/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe3")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe3")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ac88c94e-5c89-41c6-a816-090957c2ac87")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe4/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe4")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe4")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("ea8dfbb6-94db-4274-bacb-3a4712993fcb")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe5/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.Threading.Tasks;
4 | using static System.Console;
5 | using static System.Threading.Thread;
6 |
7 | namespace Chapter4.Recipe5
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | var tcs = new TaskCompletionSource();
14 |
15 | var worker = new BackgroundWorker();
16 | worker.DoWork += (sender, eventArgs) =>
17 | {
18 | eventArgs.Result = TaskMethod("Background worker", 5);
19 | };
20 |
21 | worker.RunWorkerCompleted += (sender, eventArgs) =>
22 | {
23 | if (eventArgs.Error != null)
24 | {
25 | tcs.SetException(eventArgs.Error);
26 | }
27 | else if (eventArgs.Cancelled)
28 | {
29 | tcs.SetCanceled();
30 | }
31 | else
32 | {
33 | tcs.SetResult((int)eventArgs.Result);
34 | }
35 | };
36 |
37 | worker.RunWorkerAsync();
38 |
39 | int result = tcs.Task.Result;
40 |
41 | WriteLine($"Result is: {result}");
42 | }
43 |
44 | static int TaskMethod(string name, int seconds)
45 | {
46 | WriteLine(
47 | $"Task {name} is running on a thread id " +
48 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
49 | $"{CurrentThread.IsThreadPoolThread}");
50 |
51 | Sleep(TimeSpan.FromSeconds(seconds));
52 | return 42 * seconds;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/Chapter4/Recipe5/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe5")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe5")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("36711db7-2242-4a17-8332-a8be103a94bb")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe6/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 | using static System.Console;
5 | using static System.Threading.Thread;
6 |
7 | namespace Chapter4.Recipe6
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | var cts = new CancellationTokenSource();
14 | var longTask = new Task(() => TaskMethod("Task 1", 10, cts.Token), cts.Token);
15 | WriteLine(longTask.Status);
16 | cts.Cancel();
17 | WriteLine(longTask.Status);
18 | WriteLine("First task has been cancelled before execution");
19 |
20 | cts = new CancellationTokenSource();
21 | longTask = new Task(() => TaskMethod("Task 2", 10, cts.Token), cts.Token);
22 | longTask.Start();
23 | for (int i = 0; i < 5; i++ )
24 | {
25 | Sleep(TimeSpan.FromSeconds(0.5));
26 | WriteLine(longTask.Status);
27 | }
28 | cts.Cancel();
29 | for (int i = 0; i < 5; i++)
30 | {
31 | Sleep(TimeSpan.FromSeconds(0.5));
32 | WriteLine(longTask.Status);
33 | }
34 |
35 | WriteLine($"A task has been completed with result {longTask.Result}.");
36 | }
37 |
38 | static int TaskMethod(string name, int seconds, CancellationToken token)
39 | {
40 | WriteLine(
41 | $"Task {name} is running on a thread id " +
42 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
43 | $"{CurrentThread.IsThreadPoolThread}");
44 |
45 | for (int i = 0; i < seconds; i ++)
46 | {
47 | Sleep(TimeSpan.FromSeconds(1));
48 | if (token.IsCancellationRequested) return -1;
49 | }
50 | return 42*seconds;
51 | }
52 | }
53 | }
--------------------------------------------------------------------------------
/Chapter4/Recipe6/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe6")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe6")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("8b37495d-c8c5-458c-85c3-4fdb1759f1fa")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe7/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter4.Recipe7
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task task;
13 | try
14 | {
15 | task = Task.Run(() => TaskMethod("Task 1", 2));
16 | int result = task.Result;
17 | WriteLine($"Result: {result}");
18 | }
19 | catch (Exception ex)
20 | {
21 | WriteLine($"Exception caught: {ex}");
22 | }
23 | WriteLine("----------------------------------------------");
24 | WriteLine();
25 |
26 | try
27 | {
28 | task = Task.Run(() => TaskMethod("Task 2", 2));
29 | int result = task.GetAwaiter().GetResult();
30 | WriteLine($"Result: {result}");
31 | }
32 | catch (Exception ex)
33 | {
34 | WriteLine($"Exception caught: {ex}");
35 | }
36 | WriteLine("----------------------------------------------");
37 | WriteLine();
38 |
39 | var t1 = new Task(() => TaskMethod("Task 3", 3));
40 | var t2 = new Task(() => TaskMethod("Task 4", 2));
41 | var complexTask = Task.WhenAll(t1, t2);
42 | var exceptionHandler = complexTask.ContinueWith(t =>
43 | WriteLine($"Exception caught: {t.Exception}"),
44 | TaskContinuationOptions.OnlyOnFaulted
45 | );
46 | t1.Start();
47 | t2.Start();
48 |
49 | Sleep(TimeSpan.FromSeconds(5));
50 | }
51 |
52 | static int TaskMethod(string name, int seconds)
53 | {
54 | WriteLine(
55 | $"Task {name} is running on a thread id " +
56 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
57 | $"{CurrentThread.IsThreadPoolThread}");
58 |
59 | Sleep(TimeSpan.FromSeconds(seconds));
60 | throw new Exception("Boom!");
61 | return 42 * seconds;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Chapter4/Recipe7/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe7")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe7")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("018efd13-0ef9-44ae-beca-6ad1ded1cd3a")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe8/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Threading.Tasks;
4 | using static System.Console;
5 | using static System.Threading.Thread;
6 |
7 | namespace Chapter4.Recipe8
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | var firstTask = new Task(() => TaskMethod("First Task", 3));
14 | var secondTask = new Task(() => TaskMethod("Second Task", 2));
15 | var whenAllTask = Task.WhenAll(firstTask, secondTask);
16 |
17 | whenAllTask.ContinueWith(t =>
18 | WriteLine($"The first answer is {t.Result[0]}, the second is {t.Result[1]}"),
19 | TaskContinuationOptions.OnlyOnRanToCompletion);
20 |
21 | firstTask.Start();
22 | secondTask.Start();
23 |
24 | Sleep(TimeSpan.FromSeconds(4));
25 |
26 | var tasks = new List>();
27 | for (int i = 1; i < 4; i++)
28 | {
29 | int counter = i;
30 | var task = new Task(() => TaskMethod($"Task {counter}", counter));
31 | tasks.Add(task);
32 | task.Start();
33 | }
34 |
35 | while (tasks.Count > 0)
36 | {
37 | var completedTask = Task.WhenAny(tasks).Result;
38 | tasks.Remove(completedTask);
39 | WriteLine($"A task has been completed with result {completedTask.Result}.");
40 | }
41 |
42 | Sleep(TimeSpan.FromSeconds(1));
43 | }
44 |
45 | static int TaskMethod(string name, int seconds)
46 | {
47 | WriteLine(
48 | $"Task {name} is running on a thread id " +
49 | $"{CurrentThread.ManagedThreadId}. Is thread pool thread: " +
50 | $"{CurrentThread.IsThreadPoolThread}");
51 |
52 | Sleep(TimeSpan.FromSeconds(seconds));
53 | return 42 * seconds;
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Chapter4/Recipe8/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Recipe8")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Recipe8")]
13 | [assembly: AssemblyCopyright("Copyright © 2013")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f066e26e-90b8-4fde-ad3c-384a787292ae")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/App.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Chapter4/Recipe9/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 |
3 | namespace Chapter4.Recipe9
4 | {
5 | ///
6 | /// Interaction logic for App.xaml
7 | ///
8 | public partial class App : Application
9 | {
10 | }
11 | }
--------------------------------------------------------------------------------
/Chapter4/Recipe9/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter5.Recipe1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsynchronyWithTPL();
13 | t.Wait();
14 |
15 | t = AsynchronyWithAwait();
16 | t.Wait();
17 | }
18 |
19 | static Task AsynchronyWithTPL()
20 | {
21 | Task t = GetInfoAsync("Task 1");
22 | Task t2 = t.ContinueWith(task => WriteLine(t.Result),
23 | TaskContinuationOptions.NotOnFaulted);
24 | Task t3 = t.ContinueWith(task => WriteLine(t.Exception.InnerException),
25 | TaskContinuationOptions.OnlyOnFaulted);
26 |
27 | return Task.WhenAny(t2, t3);
28 | }
29 |
30 | static async Task AsynchronyWithAwait()
31 | {
32 | try
33 | {
34 | string result = await GetInfoAsync("Task 2");
35 | WriteLine(result);
36 | }
37 | catch (Exception ex)
38 | {
39 | WriteLine(ex);
40 | }
41 | }
42 |
43 | static async Task GetInfoAsync(string name)
44 | {
45 | await Task.Delay(TimeSpan.FromSeconds(2));
46 | //throw new Exception("Boom!");
47 | return
48 | $"Task {name} is running on a thread id {CurrentThread.ManagedThreadId}." +
49 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter5.Recipe2
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsynchronousProcessing();
13 | t.Wait();
14 | }
15 |
16 | static async Task AsynchronousProcessing()
17 | {
18 | Func> asyncLambda = async name => {
19 | await Task.Delay(TimeSpan.FromSeconds(2));
20 | return
21 | $"Task {name} is running on a thread id {CurrentThread.ManagedThreadId}." +
22 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
23 | };
24 |
25 | string result = await asyncLambda("async lambda");
26 |
27 | WriteLine(result);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter5.Recipe3
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsynchronyWithTPL();
13 | t.Wait();
14 |
15 | t = AsynchronyWithAwait();
16 | t.Wait();
17 | }
18 |
19 | static Task AsynchronyWithTPL()
20 | {
21 | var containerTask = new Task(() => {
22 | Task t = GetInfoAsync("TPL 1");
23 | t.ContinueWith(task => {
24 | WriteLine(t.Result);
25 | Task t2 = GetInfoAsync("TPL 2");
26 | t2.ContinueWith(innerTask => WriteLine(innerTask.Result),
27 | TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.AttachedToParent);
28 | t2.ContinueWith(innerTask => WriteLine(innerTask.Exception.InnerException),
29 | TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent);
30 | },
31 | TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.AttachedToParent);
32 |
33 | t.ContinueWith(task => WriteLine(t.Exception.InnerException),
34 | TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.AttachedToParent);
35 | });
36 |
37 | containerTask.Start();
38 | return containerTask;
39 | }
40 |
41 | static async Task AsynchronyWithAwait()
42 | {
43 | try
44 | {
45 | string result = await GetInfoAsync("Async 1");
46 | WriteLine(result);
47 | result = await GetInfoAsync("Async 2");
48 | WriteLine(result);
49 | }
50 | catch (Exception ex)
51 | {
52 | WriteLine(ex);
53 | }
54 | }
55 |
56 | static async Task GetInfoAsync(string name)
57 | {
58 | WriteLine($"Task {name} started!");
59 | await Task.Delay(TimeSpan.FromSeconds(2));
60 | if(name == "TPL 2")
61 | throw new Exception("Boom!");
62 | return
63 | $"Task {name} is running on a thread id {CurrentThread.ManagedThreadId}." +
64 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
65 | }
66 | }
67 | }
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter5.Recipe4
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsynchronousProcessing();
13 | t.Wait();
14 | }
15 |
16 | static async Task AsynchronousProcessing()
17 | {
18 | Task t1 = GetInfoAsync("Task 1", 3);
19 | Task t2 = GetInfoAsync("Task 2", 5);
20 |
21 | string[] results = await Task.WhenAll(t1, t2);
22 | foreach (string result in results)
23 | {
24 | WriteLine(result);
25 | }
26 | }
27 |
28 | static async Task GetInfoAsync(string name, int seconds)
29 | {
30 | await Task.Delay(TimeSpan.FromSeconds(seconds));
31 | //await Task.Run(() => Thread.Sleep(TimeSpan.FromSeconds(seconds)));
32 | return
33 | $"Task {name} is running on a thread id {CurrentThread.ManagedThreadId}." +
34 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/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/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/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 | using static System.Console;
4 | using static System.Threading.Thread;
5 |
6 | namespace Chapter5.Recipe7
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Task t = AsyncTask();
13 | t.Wait();
14 |
15 | AsyncVoid();
16 | Sleep(TimeSpan.FromSeconds(3));
17 |
18 | t = AsyncTaskWithErrors();
19 | while(!t.IsFaulted)
20 | {
21 | Sleep(TimeSpan.FromSeconds(1));
22 | }
23 | WriteLine(t.Exception);
24 |
25 | //try
26 | //{
27 | // AsyncVoidWithErrors();
28 | // Thread.Sleep(TimeSpan.FromSeconds(3));
29 | //}
30 | //catch (Exception ex)
31 | //{
32 | // Console.WriteLine(ex);
33 | //}
34 |
35 | int[] numbers = {1, 2, 3, 4, 5};
36 | Array.ForEach(numbers, async number => {
37 | await Task.Delay(TimeSpan.FromSeconds(1));
38 | if (number == 3) throw new Exception("Boom!");
39 | WriteLine(number);
40 | });
41 |
42 | ReadLine();
43 | }
44 |
45 | static async Task AsyncTaskWithErrors()
46 | {
47 | string result = await GetInfoAsync("AsyncTaskException", 2);
48 | WriteLine(result);
49 | }
50 |
51 | static async void AsyncVoidWithErrors()
52 | {
53 | string result = await GetInfoAsync("AsyncVoidException", 2);
54 | WriteLine(result);
55 | }
56 |
57 | static async Task AsyncTask()
58 | {
59 | string result = await GetInfoAsync("AsyncTask", 2);
60 | WriteLine(result);
61 | }
62 |
63 | static async void AsyncVoid()
64 | {
65 | string result = await GetInfoAsync("AsyncVoid", 2);
66 | WriteLine(result);
67 | }
68 |
69 | static async Task GetInfoAsync(string name, int seconds)
70 | {
71 | await Task.Delay(TimeSpan.FromSeconds(seconds));
72 | if(name.Contains("Exception"))
73 | throw new Exception($"Boom from {name}!");
74 | return
75 | $"Task {name} is running on a thread id {CurrentThread.ManagedThreadId}." +
76 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.CompilerServices;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using static System.Console;
6 | using static System.Threading.Thread;
7 |
8 | namespace Chapter5.Recipe8
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | Task t = AsynchronousProcessing();
15 | t.Wait();
16 | }
17 |
18 | static async Task AsynchronousProcessing()
19 | {
20 | var sync = new CustomAwaitable(true);
21 | string result = await sync;
22 | WriteLine(result);
23 |
24 | var async = new CustomAwaitable(false);
25 | result = await async;
26 |
27 | WriteLine(result);
28 | }
29 |
30 | class CustomAwaitable
31 | {
32 | public CustomAwaitable(bool completeSynchronously)
33 | {
34 | _completeSynchronously = completeSynchronously;
35 | }
36 |
37 | public CustomAwaiter GetAwaiter()
38 | {
39 | return new CustomAwaiter(_completeSynchronously);
40 | }
41 |
42 | private readonly bool _completeSynchronously;
43 | }
44 |
45 | class CustomAwaiter : INotifyCompletion
46 | {
47 | private string _result = "Completed synchronously";
48 | private readonly bool _completeSynchronously;
49 |
50 | public bool IsCompleted => _completeSynchronously;
51 |
52 | public CustomAwaiter(bool completeSynchronously)
53 | {
54 | _completeSynchronously = completeSynchronously;
55 | }
56 |
57 | public string GetResult()
58 | {
59 | return _result;
60 | }
61 |
62 | public void OnCompleted(Action continuation)
63 | {
64 | ThreadPool.QueueUserWorkItem( state => {
65 | Sleep(TimeSpan.FromSeconds(1));
66 | _result = GetInfo();
67 | continuation?.Invoke();
68 | });
69 | }
70 |
71 | private string GetInfo()
72 | {
73 | return
74 | $"Task is running on a thread id {CurrentThread.ManagedThreadId}." +
75 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
76 | }
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/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/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 | using static System.Console;
8 | using static System.Threading.Thread;
9 |
10 | namespace Chapter5.Recipe9
11 | {
12 | class Program
13 | {
14 | static void Main(string[] args)
15 | {
16 | Task t = AsynchronousProcessing();
17 | t.Wait();
18 | }
19 |
20 | static async Task AsynchronousProcessing()
21 | {
22 | string result = await GetDynamicAwaitableObject(true);
23 | WriteLine(result);
24 |
25 | result = await GetDynamicAwaitableObject(false);
26 | WriteLine(result);
27 | }
28 |
29 | static dynamic GetDynamicAwaitableObject(bool completeSynchronously)
30 | {
31 | dynamic result = new ExpandoObject();
32 | dynamic awaiter = new ExpandoObject();
33 |
34 | awaiter.Message = "Completed synchronously";
35 | awaiter.IsCompleted = completeSynchronously;
36 | awaiter.GetResult = (Func)(() => awaiter.Message);
37 |
38 | awaiter.OnCompleted = (Action) ( callback =>
39 | ThreadPool.QueueUserWorkItem(state => {
40 | Sleep(TimeSpan.FromSeconds(1));
41 | awaiter.Message = GetInfo();
42 | callback?.Invoke();
43 | })
44 | );
45 |
46 | IAwaiter proxy = Impromptu.ActLike(awaiter);
47 |
48 | result.GetAwaiter = (Func) ( () => proxy );
49 |
50 | return result;
51 | }
52 |
53 | static string GetInfo()
54 | {
55 | return
56 | $"Task is running on a thread id {CurrentThread.ManagedThreadId}." +
57 | $" Is thread pool thread: {CurrentThread.IsThreadPoolThread}";
58 | }
59 | }
60 |
61 | public interface IAwaiter : INotifyCompletion
62 | {
63 | bool IsCompleted { get; }
64 |
65 | T GetResult();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Concurrent;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using static System.Console;
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 < Iterations; i++)
19 | {
20 | lock (dictionary)
21 | {
22 | dictionary[i] = Item;
23 | }
24 | }
25 | sw.Stop();
26 | WriteLine($"Writing to dictionary with a lock: {sw.Elapsed}");
27 |
28 | sw.Restart();
29 | for (int i = 0; i < Iterations; i++)
30 | {
31 | concurrentDictionary[i] = Item;
32 | }
33 | sw.Stop();
34 | WriteLine($"Writing to a concurrent dictionary: {sw.Elapsed}");
35 |
36 | sw.Restart();
37 | for (int i = 0; i < Iterations; i++)
38 | {
39 | lock (dictionary)
40 | {
41 | CurrentItem = dictionary[i];
42 | }
43 | }
44 | sw.Stop();
45 | WriteLine($"Reading from dictionary with a lock: {sw.Elapsed}");
46 |
47 | sw.Restart();
48 | for (int i = 0; i < Iterations; i++)
49 | {
50 | CurrentItem = concurrentDictionary[i];
51 | }
52 | sw.Stop();
53 | WriteLine($"Reading from a concurrent dictionary: {sw.Elapsed}");
54 | }
55 |
56 | const string Item = "Dictionary item";
57 | const int Iterations = 1000000;
58 | public static string CurrentItem;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using static System.Console;
6 |
7 | namespace Chapter6.Recipe2
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | Task t = RunProgram();
14 | t.Wait();
15 | }
16 |
17 | static async Task RunProgram()
18 | {
19 | var taskQueue = new ConcurrentQueue();
20 | var cts = new CancellationTokenSource();
21 |
22 | var taskSource = Task.Run(() => TaskProducer(taskQueue));
23 |
24 | Task[] processors = new Task[4];
25 | for (int i = 1; i <= 4; i++)
26 | {
27 | string processorId = i.ToString();
28 | processors[i-1] = Task.Run(
29 | () => TaskProcessor(taskQueue, $"Processor {processorId}", cts.Token));
30 | }
31 |
32 | await taskSource;
33 | cts.CancelAfter(TimeSpan.FromSeconds(2));
34 |
35 | await Task.WhenAll(processors);
36 | }
37 |
38 | static async Task TaskProducer(ConcurrentQueue queue)
39 | {
40 | for (int i = 1; i <= 20; i++)
41 | {
42 | await Task.Delay(50);
43 | var workItem = new CustomTask {Id = i};
44 | queue.Enqueue(workItem);
45 | WriteLine($"Task {workItem.Id} has been posted");
46 | }
47 | }
48 |
49 | static async Task TaskProcessor(
50 | ConcurrentQueue queue, string name, CancellationToken token)
51 | {
52 | CustomTask workItem;
53 | bool dequeueSuccesful = false;
54 |
55 | await GetRandomDelay();
56 | do
57 | {
58 | dequeueSuccesful = queue.TryDequeue(out workItem);
59 | if (dequeueSuccesful)
60 | {
61 | WriteLine($"Task {workItem.Id} has been processed by {name}");
62 | }
63 |
64 | await GetRandomDelay();
65 | }
66 | while (!token.IsCancellationRequested);
67 | }
68 |
69 | static Task GetRandomDelay()
70 | {
71 | int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
72 | return Task.Delay(delay);
73 | }
74 |
75 | class CustomTask
76 | {
77 | public int Id { get; set; }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using static System.Console;
6 |
7 | namespace Chapter6.Recipe3
8 | {
9 | class Program
10 | {
11 | static void Main(string[] args)
12 | {
13 | Task t = RunProgram();
14 | t.Wait();
15 | }
16 |
17 | static async Task RunProgram()
18 | {
19 | var taskStack = new ConcurrentStack();
20 | var cts = new CancellationTokenSource();
21 |
22 | var taskSource = Task.Run(() => TaskProducer(taskStack));
23 |
24 | Task[] processors = new Task[4];
25 | for (int i = 1; i <= 4; i++)
26 | {
27 | string processorId = i.ToString();
28 | processors[i - 1] = Task.Run(
29 | () => TaskProcessor(taskStack, $"Processor {processorId}", cts.Token));
30 | }
31 |
32 | await taskSource;
33 | cts.CancelAfter(TimeSpan.FromSeconds(2));
34 |
35 | await Task.WhenAll(processors);
36 | }
37 |
38 | static async Task TaskProducer(ConcurrentStack stack)
39 | {
40 | for (int i = 1; i <= 20; i++)
41 | {
42 | await Task.Delay(50);
43 | var workItem = new CustomTask { Id = i };
44 | stack.Push(workItem);
45 | WriteLine($"Task {workItem.Id} has been posted");
46 | }
47 | }
48 |
49 | static async Task TaskProcessor(
50 | ConcurrentStack stack, string name, CancellationToken token)
51 | {
52 | await GetRandomDelay();
53 | do
54 | {
55 | CustomTask workItem;
56 | bool popSuccesful = stack.TryPop(out workItem);
57 | if (popSuccesful)
58 | {
59 | WriteLine($"Task {workItem.Id} has been processed by {name}");
60 | }
61 |
62 | await GetRandomDelay();
63 | }
64 | while (!token.IsCancellationRequested);
65 | }
66 |
67 | static Task GetRandomDelay()
68 | {
69 | int delay = new Random(DateTime.Now.Millisecond).Next(1, 500);
70 | return Task.Delay(delay);
71 | }
72 |
73 | class CustomTask
74 | {
75 | public int Id { get; set; }
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/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/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Threading.Tasks;
4 | using static System.Console;
5 |
6 | namespace Chapter6.Recipe5
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | WriteLine("Using a Queue inside of BlockingCollection");
13 | WriteLine();
14 | Task t = RunProgram();
15 | t.Wait();
16 |
17 | WriteLine();
18 | WriteLine("Using a Stack inside of BlockingCollection");
19 | WriteLine();
20 | t = RunProgram(new ConcurrentStack());
21 | t.Wait();
22 | }
23 |
24 | static async Task RunProgram(IProducerConsumerCollection collection = null)
25 | {
26 | var taskCollection = new BlockingCollection();
27 | if(collection != null)
28 | taskCollection= new BlockingCollection(collection);
29 |
30 | var taskSource = Task.Run(() => TaskProducer(taskCollection));
31 |
32 | Task[] processors = new Task[4];
33 | for (int i = 1; i <= 4; i++)
34 | {
35 | string processorId = $"Processor {i}";
36 | processors[i - 1] = Task.Run(
37 | () => TaskProcessor(taskCollection, processorId));
38 | }
39 |
40 | await taskSource;
41 |
42 | await Task.WhenAll(processors);
43 | }
44 |
45 | static async Task TaskProducer(BlockingCollection collection)
46 | {
47 | for (int i = 1; i <= 20; i++)
48 | {
49 | await Task.Delay(20);
50 | var workItem = new CustomTask { Id = i };
51 | collection.Add(workItem);
52 | WriteLine($"Task {workItem.Id} has been posted");
53 | }
54 | collection.CompleteAdding();
55 | }
56 |
57 | static async Task TaskProcessor(
58 | BlockingCollection collection, string name)
59 | {
60 | await GetRandomDelay();
61 | foreach (CustomTask item in collection.GetConsumingEnumerable())
62 | {
63 | WriteLine($"Task {item.Id} has been processed by {name}");
64 | await GetRandomDelay();
65 | }
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 | }
81 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Threading;
4 | using System.Threading.Tasks;
5 | using static System.Console;
6 | using static System.Threading.Thread;
7 |
8 | namespace Chapter7.Recipe1
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | Parallel.Invoke(
15 | () => EmulateProcessing("Task1"),
16 | () => EmulateProcessing("Task2"),
17 | () => EmulateProcessing("Task3")
18 | );
19 |
20 | var cts = new CancellationTokenSource();
21 |
22 | var result = Parallel.ForEach(
23 | Enumerable.Range(1, 30),
24 | new ParallelOptions
25 | {
26 | CancellationToken = cts.Token,
27 | MaxDegreeOfParallelism = Environment.ProcessorCount,
28 | TaskScheduler = TaskScheduler.Default
29 | },
30 | (i, state) =>
31 | {
32 | WriteLine(i);
33 | if (i == 20)
34 | {
35 | state.Break();
36 | WriteLine($"Loop is stopped: {state.IsStopped}");
37 | }
38 | });
39 |
40 | WriteLine("---");
41 | WriteLine($"IsCompleted: {result.IsCompleted}");
42 | WriteLine($"Lowest break iteration: {result.LowestBreakIteration}");
43 | }
44 |
45 | static string EmulateProcessing(string taskName)
46 | {
47 | Sleep(TimeSpan.FromMilliseconds(
48 | new Random(DateTime.Now.Millisecond).Next(250, 350)));
49 | WriteLine($"{taskName} task was processed on a " +
50 | $"thread id {CurrentThread.ManagedThreadId}");
51 | return taskName;
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/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/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading;
5 | using static System.Console;
6 | using static System.Threading.Thread;
7 |
8 | namespace Chapter7.Recipe3
9 | {
10 | class Program
11 | {
12 | static void Main(string[] args)
13 | {
14 | var parallelQuery = from t in GetTypes().AsParallel()
15 | select EmulateProcessing(t);
16 |
17 | var cts = new CancellationTokenSource();
18 | cts.CancelAfter(TimeSpan.FromSeconds(3));
19 |
20 | try
21 | {
22 | parallelQuery
23 | .WithDegreeOfParallelism(Environment.ProcessorCount)
24 | .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
25 | .WithMergeOptions(ParallelMergeOptions.Default)
26 | .WithCancellation(cts.Token)
27 | .ForAll(WriteLine);
28 | }
29 | catch (OperationCanceledException)
30 | {
31 | WriteLine("---");
32 | WriteLine("Operation has been canceled!");
33 | }
34 |
35 | WriteLine("---");
36 | WriteLine("Unordered PLINQ query execution");
37 | var unorderedQuery = from i in ParallelEnumerable.Range(1, 30)
38 | select i;
39 |
40 | foreach (var i in unorderedQuery)
41 | {
42 | WriteLine(i);
43 | }
44 |
45 | WriteLine("---");
46 | WriteLine("Ordered PLINQ query execution");
47 | var orderedQuery = from i in ParallelEnumerable.Range(1, 30).AsOrdered()
48 | select i;
49 |
50 | foreach (var i in orderedQuery)
51 | {
52 | WriteLine(i);
53 | }
54 | }
55 |
56 | static string EmulateProcessing(string typeName)
57 | {
58 | Sleep(TimeSpan.FromMilliseconds(
59 | new Random(DateTime.Now.Millisecond).Next(250,350)));
60 | WriteLine($"{typeName} type was processed on a thread " +
61 | $"id {CurrentThread.ManagedThreadId}");
62 | return typeName;
63 | }
64 |
65 | static IEnumerable GetTypes()
66 | {
67 | return from assembly in AppDomain.CurrentDomain.GetAssemblies()
68 | from type in assembly.GetExportedTypes()
69 | where type.Name.StartsWith("Web")
70 | orderby type.Name.Length
71 | select type.Name;
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using static System.Console;
5 |
6 | namespace Chapter7.Recipe4
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | IEnumerable numbers = Enumerable.Range(-5, 10);
13 |
14 | var query = from number in numbers
15 | select 100 / number;
16 |
17 | try
18 | {
19 | foreach(var n in query)
20 | WriteLine(n);
21 | }
22 | catch (DivideByZeroException)
23 | {
24 | WriteLine("Divided by zero!");
25 | }
26 |
27 | WriteLine("---");
28 | WriteLine("Sequential LINQ query processing");
29 | WriteLine();
30 |
31 | var parallelQuery = from number in numbers.AsParallel()
32 | select 100 / number; ;
33 |
34 | try
35 | {
36 | parallelQuery.ForAll(WriteLine);
37 | }
38 | catch (DivideByZeroException)
39 | {
40 | WriteLine("Divided by zero - usual exception handler!");
41 | }
42 | catch (AggregateException e)
43 | {
44 | e.Flatten().Handle(ex =>
45 | {
46 | if (ex is DivideByZeroException)
47 | {
48 | WriteLine("Divided by zero - aggregate exception handler!");
49 | return true;
50 | }
51 |
52 | return false;
53 | });
54 | }
55 |
56 | WriteLine("---");
57 | WriteLine("Parallel LINQ query processing and results merging");
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/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/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/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/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 | using static System.Console;
7 | using static System.Threading.Thread;
8 |
9 | namespace Chapter8.Recipe1
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | foreach (int i in EnumerableEventSequence())
16 | {
17 | Write(i);
18 | }
19 |
20 | WriteLine();
21 | WriteLine("IEnumerable");
22 |
23 | IObservable o = EnumerableEventSequence().ToObservable();
24 | using (IDisposable subscription = o.Subscribe(Write))
25 | {
26 | WriteLine();
27 | WriteLine("IObservable");
28 | }
29 |
30 | o = EnumerableEventSequence().ToObservable()
31 | .SubscribeOn(TaskPoolScheduler.Default);
32 | using (IDisposable subscription = o.Subscribe(Write))
33 | {
34 | WriteLine();
35 | WriteLine("IObservable async");
36 | ReadLine();
37 | }
38 | }
39 |
40 | static IEnumerable EnumerableEventSequence()
41 | {
42 | for (int i = 0; i < 10; i++)
43 | {
44 | Sleep(TimeSpan.FromSeconds(0.5));
45 | yield return i;
46 | }
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/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/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 static System.Console;
7 | using static System.Threading.Thread;
8 |
9 | namespace Chapter8.Recipe2
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | var observer = new CustomObserver();
16 |
17 | var goodObservable = new CustomSequence(new[] {1, 2, 3, 4, 5});
18 | var badObservable = new CustomSequence(null);
19 |
20 | using (IDisposable subscription = goodObservable.Subscribe(observer))
21 | {
22 | }
23 |
24 | using (IDisposable subscription = goodObservable
25 | .SubscribeOn(TaskPoolScheduler.Default).Subscribe(observer))
26 | {
27 | Sleep(TimeSpan.FromMilliseconds(100));
28 | WriteLine("Press ENTER to continue");
29 | ReadLine();
30 | }
31 |
32 | using (IDisposable subscription = badObservable
33 | .SubscribeOn(TaskPoolScheduler.Default).Subscribe(observer))
34 | {
35 | Sleep(TimeSpan.FromMilliseconds(100));
36 | WriteLine("Press ENTER to continue");
37 | ReadLine();
38 | }
39 | }
40 |
41 | class CustomObserver : IObserver
42 | {
43 | public void OnNext(int value)
44 | {
45 | WriteLine($"Next value: {value}; Thread Id: {CurrentThread.ManagedThreadId}");
46 | }
47 |
48 | public void OnError(Exception error)
49 | {
50 | WriteLine($"Error: {error.Message}");
51 | }
52 |
53 | public void OnCompleted()
54 | {
55 | WriteLine("Completed");
56 | }
57 | }
58 |
59 | class CustomSequence : IObservable
60 | {
61 | private readonly IEnumerable _numbers;
62 |
63 | public CustomSequence(IEnumerable numbers)
64 | {
65 | _numbers = numbers;
66 | }
67 | public IDisposable Subscribe(IObserver observer)
68 | {
69 | foreach (var number in _numbers)
70 | {
71 | observer.OnNext(number);
72 | }
73 | observer.OnCompleted();
74 | return Disposable.Empty;
75 | }
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/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/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/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/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reactive.Linq;
3 | using static System.Console;
4 |
5 | namespace Chapter8.Recipe5
6 | {
7 | class Program
8 | {
9 | static void Main(string[] args)
10 | {
11 | IObservable sequence = Observable.Interval(
12 | TimeSpan.FromMilliseconds(50)).Take(21);
13 |
14 | var evenNumbers = from n in sequence
15 | where n % 2 == 0
16 | select n;
17 |
18 | var oddNumbers = from n in sequence
19 | where n % 2 != 0
20 | select n;
21 |
22 | var combine = from n in evenNumbers.Concat(oddNumbers)
23 | select n;
24 |
25 | var nums = (from n in combine
26 | where n % 5 == 0
27 | select n)
28 | .Do(n => WriteLine($"------Number {n} is processed in Do method"));
29 |
30 | using (var sub = OutputToConsole(sequence, 0))
31 | using (var sub2 = OutputToConsole(combine, 1))
32 | using (var sub3 = OutputToConsole(nums, 2))
33 | {
34 | WriteLine("Press enter to finish the demo");
35 | ReadLine();
36 | }
37 | }
38 |
39 | static IDisposable OutputToConsole(IObservable sequence, int innerLevel)
40 | {
41 | string delimiter = innerLevel == 0
42 | ? string.Empty
43 | : new string('-', innerLevel*3);
44 |
45 | return sequence.Subscribe(
46 | obj => WriteLine($"{delimiter}{obj}")
47 | , ex => WriteLine($"Error: {ex.Message}")
48 | , () => WriteLine($"{delimiter}Completed")
49 | );
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/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/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/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/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/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/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 with C# Cookbook - Second Edition
2 | ======================
3 |
4 | Here are the code samples from my book Multithreading with C# Cookbook - Second Edition, converted to Visual Studio 2015 and Windows 10
5 | You can use free Visual Studio 2015 Community to work with the code, it is able to run all the recipes provided.
6 |
7 | This is the code repository for [Multithreading with C# Cookbook - Second Edition](https://www.packtpub.com/application-development/multithreading-c-cookbook-second-edition?utm_source=github&utm_medium=repository&utm_campaign=9781785881251), published by Packt. It contains all the supporting project files necessary to work through the book from start to finish.
8 |
9 |
10 |
11 |
12 |
13 | In case you have any suggestions or bug reports please send them to multithreading@eugeneagafonov.com.
14 |
--------------------------------------------------------------------------------