Skip to content

Commit d184ce2

Browse files
committed
summarizing examples ExamRef70-483#2
1 parent a9ca38a commit d184ce2

File tree

55 files changed

+250
-242
lines changed
  • LISTING 1-18 Creating threads/LISTING 1-18 Creating threads
  • LISTING 1-19 Using ThreadStart/LISTING 1-19 Using ThreadStart
  • LISTING 1-20 Threads and lambda expressions/LISTING 1-20 Threads and lambda expressions
  • LISTING 1-21 ParameterizedThreadStart/LISTING 1-21 ParameterizedThreadStart
  • LISTING 1-22 thread lambda parameters/LISTING 1-22 thread lambda parameters
  • LISTING 1-23 aborting a thread/LISTING 1-23 aborting a thread
  • LISTING 1-24 shared flag variable/LISTING 1-24 shared flag variable
  • LISTING 1-25 Using join/LISTING 1-25 Using join
  • LISTING 1-26 ThreadLocal/LISTING 1-26 ThreadLocal
  • LISTING 1-28 Thread pool/LISTING 1-28 Thread pool
  • LISTING 1-29 Blocking the user interface/LISTING 1-29 Blocking the user interface
  • LISTING 1-30 Using a task/LISTING 1-30 Using a task
  • LISTING 1-31 Updating the UI/LISTING 1-31 Updating the UI
  • LISTING 1-32 Using async/LISTING 1-32 Using async
  • LISTING 1-33 Exceptions and async/LISTING 1-33 Exceptions and async
  • LISTING 1-34 Awaiting parallel tasks/LISTING 1-34 Awaiting parallel tasks
  • LISTING 1-35 Using BlockingCollection/LISTING 1-35 Using BlockingCollection
  • LISTING 1-36 Block ConcurrentStack/LISTING 1-36 Block ConcurrentStack
  • LISTING 1-37 Concurrent queue/LISTING 1-37 Concurrent queue
  • LISTING 1-38 Concurrent stack/LISTING 1-38 Concurrent stack
  • LISTING 1-39 Concurrent bag/LISTING 1-39 Concurrent bag
  • LISTING 1-40 Concurrent dictionary/LISTING 1-40 Concurrent dictionary
  • LISTING 1-42 Bad task interaction/LISTING 1-42 Bad task interaction
  • LISTING 1-45 Using monitors/LISTING 1-45 Using monitors
  • LISTING 1-53 do while loops/LISTING 1-53 do while loops
  • LISTING 1-57 uppercase Person/LISTING 1-57 uppercase Person
  • LISTING 1-62 The switch construction/LISTING 1-62 The switch construction
  • LISTING 1-64 Expression evaluation/LISTING 1-64 Expression evaluation
  • LISTING 1-65 Null conditional/LISTING 1-65 Null conditional
  • LISTING 1-65 Publish and subscribe/LISTING 1-65 Publish and subscribe
  • LISTING 1-66 Unsubscribing/LISTING 1-66 Unsubscribing
  • LISTING 1-67 Event based alarm/LISTING 1-67 Event based alarm
  • LISTING 1-68 EventHandler alarm/LISTING 1-68 EventHandler alarm
  • LISTING 1-70 Aggregating exceptions/LISTING 1-70 Aggregating exceptions
  • LISTING 1-71 Create delegates/LISTING 1-71 Create delegates
  • LISTING 1-72 Lambda expressions/LISTING 1-72 Lambda expressions
  • LISTING 1-73 Closures/LISTING 1-73 Closures
  • LISTING 1-74 Built in delegates/LISTING 1-74 Built in delegates
  • LISTING 1-75 lambda expression task/LISTING 1-75 lambda expression task
  • LISTING 1-76 try catch/LISTING 1-76 try catch
  • LISTING 1-77 Exception object/LISTING 1-77 Exception object
  • LISTING 1-78 Exception types/LISTING 1-78 Exception types
  • LISTING 1-79 The finally block/LISTING 1-79 The finally block
  • LISTING 1-80 Throwing an exception/LISTING 1-80 Throwing an exception
  • LISTING 1-81 Custom exceptions/LISTING 1-81 Custom exceptions
  • LISTING 1-82 Conditional clauses/LISTING 1-82 Conditional clauses
  • LISTING 1-83 Inner exceptions/LISTING 1-83 Inner exceptions
  • LISTING 1-84 Aggregate exceptions/LISTING 1-84 Aggregate exceptions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+250
-242
lines changed

LISTING 1-18 Creating threads/LISTING 1-18 Creating threads/Program.cs

+1-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void Main(string[] args)
1515
{
1616
/*
1717
18-
task vs Thread:
18+
Task vs Thread:
1919
2020
• A Task object represents an item of work to be performed.
2121
• A Thread object represents a process running within the Operating System.
@@ -61,18 +61,6 @@ uncaught exception or when the operating system terminates it.
6161

6262
Console.WriteLine("Press a key to end.");
6363
Console.ReadKey();
64-
65-
/*
66-
67-
Hello from the thread.
68-
Press a key to end.
69-
70-
OR
71-
72-
Press a key to end.
73-
Hello from the thread.
74-
75-
*/
7664
}
7765
}
7866
}

LISTING 1-19 Using ThreadStart/LISTING 1-19 Using ThreadStart/Program.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,13 @@ static void Main(string[] args)
1818
Use ThreadStart delegate to specify the method to be executed by the thread.
1919
2020
*/
21+
2122
ThreadStart ts = new ThreadStart(ThreadHello);
2223
Thread thread = new Thread(ts);
2324
thread.Start();
2425

2526
Console.WriteLine("Press a key to end.");
2627
Console.ReadKey();
27-
28-
/*
29-
30-
Hello from the thread.
31-
Press a key to end.
32-
33-
OR
34-
35-
Press a key to end.
36-
Hello from the thread.
37-
38-
*/
3928
}
4029
}
4130
}

LISTING 1-20 Threads and lambda expressions/LISTING 1-20 Threads and lambda expressions/Program.cs

+1-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ writeline method and displays "Press a key to end.".
1717
• After that the background thread gets control and prints "Hello from the thread.".
1818
1919
*/
20+
2021
Thread thread = new Thread(() =>
2122
{
2223
Console.WriteLine("Hello from the thread.");
@@ -26,13 +27,6 @@ writeline method and displays "Press a key to end.".
2627

2728
Console.WriteLine("Press a key to end.");
2829
Console.ReadKey();
29-
30-
/*
31-
32-
Press a key to end.
33-
Hello from the thread.
34-
35-
*/
3630
}
3731
}
3832
}

LISTING 1-21 ParameterizedThreadStart/LISTING 1-21 ParameterizedThreadStart/Program.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ static void WorkOnData(object data)
1010
Console.WriteLine($"Working on: {data}.");
1111
Thread.Sleep(1000);
1212
}
13+
1314
static void Main(string[] args)
1415
{
1516
/*
@@ -20,19 +21,13 @@ static void Main(string[] args)
2021
• The object to be passed into the thread is then placed in the Start method.
2122
2223
*/
24+
2325
ParameterizedThreadStart ps = new ParameterizedThreadStart(WorkOnData);
2426
Thread thread = new Thread(ps);
2527
thread.Start(99);
2628

2729
Console.WriteLine("Press a key to end.");
2830
Console.ReadKey();
29-
30-
/*
31-
32-
Press a key to end.
33-
Working on: 99.
34-
35-
*/
3631
}
3732
}
3833
}

LISTING 1-22 thread lambda parameters/LISTING 1-22 thread lambda parameters/Program.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace LISTING_1_22_thread_lambda_parameters
95
{
@@ -30,22 +26,15 @@ This means that there is no way to be sure at compile time that thread initializ
3026
with a particular type of data.
3127
3228
*/
29+
3330
Thread thread = new Thread((data) =>
3431
{
3532
WorkOnData(data);
3633
});
3734
thread.Start(99);
38-
//thread.Start("99");
3935

4036
Console.WriteLine("Press a key to end.");
4137
Console.ReadKey();
42-
43-
/*
44-
45-
Press a key to end.
46-
Working on: 99
47-
48-
*/
4938
}
5039
}
5140
}

LISTING 1-23 aborting a thread/LISTING 1-23 aborting a thread/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static void Main(string[] args)
2828
files open and resources assigned.
2929
3030
*/
31+
3132
tickThread.Abort();
3233

3334
Console.WriteLine("Press a key to exit.");

LISTING 1-24 shared flag variable/LISTING 1-24 shared flag variable/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static void Main(string[] args)
1818
A better way to abort a thread is to use a shared flag variable.
1919
2020
*/
21+
2122
while (tickRunning)
2223
{
2324
Console.WriteLine("Tick");

LISTING 1-25 Using join/LISTING 1-25 Using join/Program.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using System.Threading;
6-
using System.Threading.Tasks;
73

84
namespace LISTING_1_25_Using_join
95
{
@@ -29,7 +25,8 @@ The join method allows two threads to synchronize.
2925
completes.
3026
3127
*/
32-
threadToWaitFor.Join(); // threadMain calls join on (thus waits for) threadToWaitFor
28+
29+
threadToWaitFor.Join(); // Thread.Current calls join on (thus waits for) threadToWaitFor
3330

3431
Console.WriteLine("Press a key to exit.");
3532
Console.ReadKey();

LISTING 1-26 ThreadLocal/LISTING 1-26 ThreadLocal/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Threading;
33

4-
namespace LISTING_1_27_ThreadLocal
4+
namespace LISTING_1_26_ThreadLocal
55
{
66
class Program
77
{

LISTING 1-28 Thread pool/LISTING 1-28 Thread pool/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from the thread pool.
1717
1818
When the thread completes, the thread is returned to the pool for use by another process.
1919
20-
The ThreadPool provides a method QueueUserWOrkItem, which allocates a thread to run the supplied item of work.
20+
The ThreadPool provides a method QueueUserWorkItem, which allocates a thread to run the supplied item of work.
2121
2222
The item of work is supplied as a WaitCallBack delegate.
2323
@@ -36,7 +36,7 @@ A program that creates a large number of individual threads can easily overwhelm
3636
There are some situations when using the ThreadPool is not an good idea:
3737
3838
• If you create a large number of threads that may be idle for a very long time, this may block the ThreadPool,
39-
because the ThreadPool only contains a fiinite number of threads.
39+
because the ThreadPool only contains a finite number of threads.
4040
• You cannot manage the priority of threads in the ThreadPool.
4141
• Threads in the ThreadPool have background priority. You cannot obtain a thread with foreground priority from
4242
the ThreadPool.

LISTING 1-29 Blocking the user interface/LISTING 1-29 Blocking the user interface/App.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Runtime.InteropServices.WindowsRuntime;
62
using Windows.ApplicationModel;
73
using Windows.ApplicationModel.Activation;
8-
using Windows.Foundation;
9-
using Windows.Foundation.Collections;
104
using Windows.UI.Xaml;
115
using Windows.UI.Xaml.Controls;
12-
using Windows.UI.Xaml.Controls.Primitives;
13-
using Windows.UI.Xaml.Data;
14-
using Windows.UI.Xaml.Input;
15-
using Windows.UI.Xaml.Media;
166
using Windows.UI.Xaml.Navigation;
177

188
namespace Random_Averages
@@ -97,4 +87,4 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
9787
deferral.Complete();
9888
}
9989
}
100-
}
90+
}

LISTING 1-29 Blocking the user interface/LISTING 1-29 Blocking the user interface/MainPage.xaml.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
3-
using Windows.UI.Core;
4-
using Windows.UI.Popups;
52
using Windows.UI.Xaml;
63
using Windows.UI.Xaml.Controls;
74

@@ -48,4 +45,4 @@ private void StartButton_Click(object sender, RoutedEventArgs e)
4845
ResultTextBlock.Text = "Result: " + computeAverages(noOfValues);
4946
}
5047
}
51-
}
48+
}

LISTING 1-30 Using a task/LISTING 1-30 Using a task/App.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Runtime.InteropServices.WindowsRuntime;
62
using Windows.ApplicationModel;
73
using Windows.ApplicationModel.Activation;
8-
using Windows.Foundation;
9-
using Windows.Foundation.Collections;
104
using Windows.UI.Xaml;
115
using Windows.UI.Xaml.Controls;
12-
using Windows.UI.Xaml.Controls.Primitives;
13-
using Windows.UI.Xaml.Data;
14-
using Windows.UI.Xaml.Input;
15-
using Windows.UI.Xaml.Media;
166
using Windows.UI.Xaml.Navigation;
177

188
namespace Random_Averages
@@ -97,4 +87,4 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
9787
deferral.Complete();
9888
}
9989
}
100-
}
90+
}

LISTING 1-30 Using a task/LISTING 1-30 Using a task/MainPage.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ private void StartButton_Click(object sender, RoutedEventArgs e)
5757
});
5858
}
5959
}
60-
}
60+
}

LISTING 1-31 Updating the UI/LISTING 1-31 Updating the UI/App.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Runtime.InteropServices.WindowsRuntime;
62
using Windows.ApplicationModel;
73
using Windows.ApplicationModel.Activation;
8-
using Windows.Foundation;
9-
using Windows.Foundation.Collections;
104
using Windows.UI.Xaml;
115
using Windows.UI.Xaml.Controls;
12-
using Windows.UI.Xaml.Controls.Primitives;
13-
using Windows.UI.Xaml.Data;
14-
using Windows.UI.Xaml.Input;
15-
using Windows.UI.Xaml.Media;
166
using Windows.UI.Xaml.Navigation;
177

188
namespace Random_Averages
@@ -97,4 +87,4 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
9787
deferral.Complete();
9888
}
9989
}
100-
}
90+
}

LISTING 1-31 Updating the UI/LISTING 1-31 Updating the UI/MainPage.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ private void StartButton_Click(object sender, RoutedEventArgs e)
5454
});
5555
}
5656
}
57-
}
57+
}

LISTING 1-32 Using async/LISTING 1-32 Using async/App.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Runtime.InteropServices.WindowsRuntime;
62
using Windows.ApplicationModel;
73
using Windows.ApplicationModel.Activation;
8-
using Windows.Foundation;
9-
using Windows.Foundation.Collections;
104
using Windows.UI.Xaml;
115
using Windows.UI.Xaml.Controls;
12-
using Windows.UI.Xaml.Controls.Primitives;
13-
using Windows.UI.Xaml.Data;
14-
using Windows.UI.Xaml.Input;
15-
using Windows.UI.Xaml.Media;
166
using Windows.UI.Xaml.Navigation;
177

188
namespace Random_Averages
@@ -97,4 +87,4 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
9787
deferral.Complete();
9888
}
9989
}
100-
}
90+
}

LISTING 1-32 Using async/LISTING 1-32 Using async/MainPage.xaml.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Threading.Tasks;
3-
using Windows.UI.Core;
43
using Windows.UI.Xaml;
54
using Windows.UI.Xaml.Controls;
65

@@ -18,7 +17,6 @@ public MainPage()
1817
this.InitializeComponent();
1918
}
2019

21-
2220
private double computeAverages(long noOfValues)
2321
{
2422
double total = 0;
@@ -32,7 +30,6 @@ private double computeAverages(long noOfValues)
3230
return total / noOfValues;
3331
}
3432

35-
3633
private Task<double> asyncComputeAverages(long noOfValues)
3734
{
3835
return Task<double>.Run(() =>
@@ -71,4 +68,4 @@ private async void StartButton_Click(object sender, RoutedEventArgs e)
7168
ResultTextBlock.Text = "Result: " + result.ToString();
7269
}
7370
}
74-
}
71+
}

LISTING 1-33 Exceptions and async/LISTING 1-33 Exceptions and async/App.xaml.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Runtime.InteropServices.WindowsRuntime;
62
using Windows.ApplicationModel;
73
using Windows.ApplicationModel.Activation;
8-
using Windows.Foundation;
9-
using Windows.Foundation.Collections;
104
using Windows.UI.Xaml;
115
using Windows.UI.Xaml.Controls;
12-
using Windows.UI.Xaml.Controls.Primitives;
13-
using Windows.UI.Xaml.Data;
14-
using Windows.UI.Xaml.Input;
15-
using Windows.UI.Xaml.Media;
166
using Windows.UI.Xaml.Navigation;
177

188
namespace Webpage_Viewer
@@ -97,4 +87,4 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
9787
deferral.Complete();
9888
}
9989
}
100-
}
90+
}

0 commit comments

Comments
 (0)