Skip to content

Commit 2fe8fcf

Browse files
committed
summarizing examples ExamRef70-483#1
1 parent bb6aa4b commit 2fe8fcf

File tree

18 files changed

+250
-189
lines changed
  • LISTING 1-1 Parallel Invoke/LISTING 1-1 Parallel Invoke
  • LISTING 1-10 Exceptions in PLINQ/LISTING 1-10 Exceptions in PLINQ
  • LISTING 1-11 Create a task/LISTING 1-11 Create a task
  • LISTING 1-12 Run a task/LISTING 1-12 Run a task
  • LISTING 1-13 Task Factory/LISTING 1-13 Task Factory
  • LISTING 1-13 Task returning a value/LISTING 1-13 Task returning a value
  • LISTING 1-14 Task waitall/LISTING 1-14 Task waitall
  • LISTING 1-15 Continuation tasks/LISTING 1-15 Continuation tasks
  • LISTING 1-16 Continuation options/LISTING 1-16 Continuation options
  • LISTING 1-2 ParallelForEach in use/LISTING 1-2 ParallelForEach in use
  • LISTING 1-3 ParallelFor in use/LISTING 1-3 ParallelFor in use
  • LISTING 1-4 Managing a parallel For loop/LISTING 1-4 Managing a parallel For loop
  • LISTING 1-5 A parallel LINQ query/LISTING 1-5 A parallel LINQ query
  • LISTING 1-6 A informing parallelization/LISTING 1-6 A informing parallelization
  • LISTING 1-7 Using AsOrdered/LISTING 1-7 Using AsOrdered
  • LISTING 1-8 Sequential elements/LISTING 1-8 Sequential elements
  • LISTING 1-9 Using the ForAll method/LISTING 1-9 Using the ForAll method

18 files changed

+250
-189
lines changed

Diff for: All.sln

+5
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LISTING 1-31 Updating the
575575
EndProject
576576
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LISTING 1-32 Using async", "LISTING 1-32 Using async\LISTING 1-32 Using async\LISTING 1-32 Using async.csproj", "{A9B754B1-55F5-46EE-943E-D0407EEB484A}"
577577
EndProject
578+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{79A31FC8-3FD2-4ACF-9C61-7F54B46F6854}"
579+
ProjectSection(SolutionItems) = preProject
580+
Summary.txt = Summary.txt
581+
EndProjectSection
582+
EndProject
578583
Global
579584
GlobalSection(SolutionConfigurationPlatforms) = preSolution
580585
Custom|Any CPU = Custom|Any CPU

Diff for: LISTING 1-1 Parallel Invoke/LISTING 1-1 Parallel Invoke/Program.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ class Program
99
static void Task1()
1010
{
1111
Console.WriteLine("Task 1 starting");
12-
Thread.Sleep(2000);
13-
//Thread.Sleep(1000);
12+
//Thread.Sleep(2000);
13+
Thread.Sleep(1000);
1414
Console.WriteLine("Task 1 ending");
1515
}
1616

1717
static void Task2()
1818
{
1919
Console.WriteLine("Task 2 starting");
20+
//Thread.Sleep(2000);
2021
Thread.Sleep(1000);
2122
Console.WriteLine("Task 2 ending");
2223
}
@@ -30,10 +31,19 @@ static void Main(string[] args)
3031
// An action delegate can be replaced with a lambda expression.
3132
// Note that you have no control over the order in which the tasks are started or which processor they are
3233
// assigned to (e.g. Task1 might finish before Task2 and vice versa).
34+
35+
Action action1a = () => Task1();
36+
Action action2a = () => Task2();
37+
//Parallel.Invoke(action1a, action2a);
38+
39+
Action action1b = new Action(() => Task1());
40+
Action action2b = new Action(() => Task2());
41+
//Parallel.Invoke(action1b, action2b);
42+
3343
Parallel.Invoke(() => Task1(), () => Task2());
3444

3545
Console.WriteLine("Finished processing. Press a key to end.");
3646
Console.ReadKey();
3747
}
3848
}
39-
}
49+
}

Diff for: LISTING 1-10 Exceptions in PLINQ/LISTING 1-10 Exceptions in PLINQ/Program.cs

+22-25
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,57 @@ namespace LISTING_1_10_Exceptions_in_PLINQ
66
class Program
77
{
88
public static bool CheckCity(string city)
9-
{
10-
if (city == "")
11-
throw new ArgumentException(city);
12-
return city == "Seattle";
13-
}
14-
15-
public static bool CheckCity(string name, string city)
169
{
1710
if (city == "")
1811
{
19-
Console.WriteLine($"no city for {name}");
2012
throw new ArgumentException(city);
2113
}
14+
2215
return city == "Seattle";
2316
}
2417

2518
class Person
2619
{
2720
public string Name { get; set; }
21+
2822
public string City { get; set; }
2923
}
3024

3125
static void Main(string[] args)
3226
{
33-
Person[] people = new Person[] {
34-
//new Person { Name = "Alan", City = "Hull" },
35-
new Person { Name = "Alan", City = "" },
36-
new Person { Name = "Beryl", City = "Seattle" },
37-
new Person { Name = "Charles", City = "London" },
27+
Person[] people = new Person[]
28+
{
29+
new Person { Name = "Alan", City = "Hull" },
30+
new Person { Name = "Henry", City = "Seattle" },
31+
new Person { Name = "Charles", City = "" },
32+
new Person { Name = "Isaac", City = "Seattle" },
33+
new Person { Name = "Gordon", City = "Hull" },
3834
new Person { Name = "David", City = "Seattle" },
3935
new Person { Name = "Eddy", City = "" },
4036
new Person { Name = "Fred", City = "" },
41-
new Person { Name = "Gordon", City = "Hull" },
42-
new Person { Name = "Henry", City = "Seattle" },
43-
new Person { Name = "Isaac", City = "Seattle" },
44-
new Person { Name = "James", City = "London" }};
37+
new Person { Name = "Beryl", City = "Seattle" },
38+
new Person { Name = "James", City = "" }
39+
};
4540

4641
try
4742
{
4843
// An AggregateException is thrown when all queries are completed if any query generates an exception.
49-
var result = from person in
50-
people.AsParallel()
51-
//where CheckCity(person.City)
52-
where CheckCity(person.Name, person.City)
53-
select person;
54-
result.ForAll(person => Console.WriteLine(person.Name));
44+
45+
Console.WriteLine("parallel query:");
46+
var resultP = from person in people.AsParallel()
47+
where CheckCity(person.City)
48+
//orderby person.Name
49+
select person;
50+
51+
resultP.ForAll(person => Console.WriteLine(person.Name));
5552
}
5653
catch (AggregateException e)
5754
{
58-
Console.WriteLine(e.InnerExceptions.Count + " exception(s)."); // might be > 1
55+
Console.WriteLine(e.InnerExceptions.Count + " inner exception(s)."); // 1-3 runtime vs 4 when debugging, why?
5956
}
6057

6158
Console.WriteLine("Finished processing. Press a key to end.");
6259
Console.ReadKey();
6360
}
6461
}
65-
}
62+
}

Diff for: LISTING 1-11 Create a task/LISTING 1-11 Create a task/Program.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void DoWork()
1616
static void Main(string[] args)
1717
{
1818
// Create a task, start it and wait for it to complete.
19+
1920
Task newTask = new Task(() => DoWork());
2021
Console.WriteLine("Task start (begin)");
2122
newTask.Start();
@@ -24,6 +25,9 @@ static void Main(string[] args)
2425
newTask.Wait();
2526
Console.WriteLine("Task wait (end)");
2627

28+
Console.WriteLine("Finished processing. Press a key to end.");
29+
Console.ReadKey();
30+
2731
/*
2832
2933
Task start (begin)
@@ -32,8 +36,9 @@ Task wait (begin)
3236
Work starting
3337
Work finished
3438
Task wait (end)
39+
Finished processing. Press a key to end.
3540
3641
*/
3742
}
3843
}
39-
}
44+
}

Diff for: LISTING 1-12 Run a task/LISTING 1-12 Run a task/Program.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,32 @@ public static void DoWork()
1313
Console.WriteLine("Work finished");
1414
}
1515

16-
// Run a task (create and start) and wait for it to complete.
1716
static void Main(string[] args)
1817
{
19-
Console.WriteLine("Task start (begin)");
18+
// Run a task (create and start) and wait for it to complete.
19+
20+
Console.WriteLine("Task run (begin)");
2021
Task newTask = Task.Run(() => DoWork());
21-
Console.WriteLine("Task start (end)");
22+
Console.WriteLine("Task run (end)");
2223
Console.WriteLine("Task wait (begin)");
2324
newTask.Wait();
2425
Console.WriteLine("Task wait (end)");
2526

27+
Console.WriteLine("Finished processing. Press a key to end.");
28+
Console.ReadKey();
29+
2630
/*
2731
28-
Task start (begin)
29-
Task start (end)
32+
Task run (begin)
33+
Task run (end)
3034
Task wait (begin)
3135
Work starting
3236
Work finished
3337
Task wait (end)
3438
39+
Finished processing. Press a key to end.
40+
3541
*/
3642
}
3743
}
38-
}
44+
}

Diff for: LISTING 1-13 Task Factory/LISTING 1-13 Task Factory/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ static void DoWork()
5353
Console.WriteLine("Result: " + total); // 12497500
5454
}
5555
}
56-
}
56+
}

Diff for: LISTING 1-13 Task returning a value/LISTING 1-13 Task returning a value/Program.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,30 @@ static void Main(string[] args)
2424
// You can create your own task scheduler or run a task scheduler in the synchronization context of another
2525
// processor.
2626
// You can also create your own TaskFactory if you want to create a number of tasks with the same
27-
// configuration.
27+
// configuration.
28+
29+
Console.WriteLine("Task run (begin)");
2830
Task<int> task = Task.Run(() =>
2931
{
3032
return CalculateResult();
3133
});
34+
Console.WriteLine("Task run (end)");
3235

33-
Console.WriteLine(task.Result);
34-
36+
Console.WriteLine(task.Result); // await
37+
3538
Console.WriteLine("Finished processing. Press a key to end.");
3639
Console.ReadKey();
40+
41+
/*
42+
43+
Task run (begin)
44+
Task run (end)
45+
Work starting
46+
Work finished
47+
99
48+
Finished processing. Press a key to end.
49+
50+
*/
3751
}
3852
}
39-
}
53+
}

Diff for: LISTING 1-14 Task waitall/LISTING 1-14 Task waitall/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ static void Main(string[] args)
3636
Console.ReadKey();
3737
}
3838
}
39-
}
39+
}

Diff for: LISTING 1-15 Continuation tasks/LISTING 1-15 Continuation tasks/Program.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ static void Main(string[] args)
2424
// If the antecedent task produces a result, it can be supplied as an input to the continuation task.
2525
// Continuation tasks can be used to create a "pipeline" of operations, with each successive stage starting
2626
// when the preceding one ends.
27+
2728
// A Task object exposes a ContinueWith method that can be used to specify a continuation task.
2829
// The lambda expression that executes the continuation task is provided with a reference to the antecedent
2930
// task, which it can use to determine if the antecedent completed successfully.
3031
// You can add continuation tasks to tasks that deliver a result, in which case the continuation task can
3132
// use the Result property of the antecedent task to obtain its input data.
33+
3234
Task task = Task.Run(() => HelloTask());
33-
task.ContinueWith( (prevTask) => WorldTask());
35+
task.ContinueWith((prevTask) => WorldTask());
3436

3537
Console.WriteLine("Finished processing. Press a key to end.");
3638
Console.ReadKey();
3739
}
3840
}
39-
}
41+
}

Diff for: LISTING 1-16 Continuation options/LISTING 1-16 Continuation options/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ static void Main(string[] args)
3939
Console.ReadKey();
4040
}
4141
}
42-
}
42+
}

Diff for: LISTING 1-2 ParallelForEach in use/LISTING 1-2 ParallelForEach in use/Program.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -16,20 +17,22 @@ static void WorkOnItem(object item)
1617

1718
static void Main(string[] args)
1819
{
19-
var items = Enumerable.Range(0, 500);
20+
//var items = Enumerable.Range(0, 10);
21+
IEnumerable<int> items = Enumerable.Range(0, 10);
2022

2123
// The Parallel.ForEach method accepts two parameters.
2224
// The first parameter is an IEnumerable collection.
2325
// The second parameter provides the action to be performed on each item in the collection.
2426
// Note that the tasks are not completed in the same order that they were started.
25-
Parallel.ForEach(items, item =>
26-
{
27-
WorkOnItem(item);
28-
});
27+
28+
Action<int> itemAction = new Action<int>((item) => WorkOnItem(item));
29+
//Parallel.ForEach(items, itemAction);
30+
31+
//Parallel.ForEach<int>(items, item => { WorkOnItem(item); });
32+
Parallel.ForEach(items, item => { WorkOnItem(item); });
2933

3034
Console.WriteLine("Finished processing. Press a key to end.");
3135
Console.ReadKey();
3236
}
33-
3437
}
35-
}
38+
}

Diff for: LISTING 1-3 ParallelFor in use/LISTING 1-3 ParallelFor in use/Program.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ static void WorkOnItem(object item)
1616

1717
static void Main(string[] args)
1818
{
19-
var items = Enumerable.Range(0, 500).ToArray();
19+
//var items = Enumerable.Range(0, 10).ToArray();
20+
int[] items = Enumerable.Range(0, 10).ToArray();
2021

2122
// The Parallel.For method accepts three parameters.
2223
// The first and second parameter represent start and end index for iteration of the collection.
2324
// The third parameter represents the delegate that is invoked once per iteration.
2425
// Note that the tasks are not completed in the same order that they were started.
25-
Parallel.For(0, items.Length, i =>
26-
{
27-
WorkOnItem(items[i]);
28-
});
26+
27+
Action<int> itemAction = new Action<int>((item) => WorkOnItem(item));
28+
//Parallel.For(0, items.Length, itemAction);
29+
30+
Parallel.For(0, items.Length, i => { WorkOnItem(items[i]); });
2931

3032
Console.WriteLine("Finished processing. Press a key to end.");
3133
Console.ReadKey();
3234
}
3335
}
34-
}
36+
}

0 commit comments

Comments
 (0)