3
3
using System . Threading . Tasks ;
4
4
5
5
namespace LISTING_1_13_Task_Factory
6
+ {
7
+ /*
6
8
7
- // This is a bonus example program that shows how to use the TaskFactory object.
8
- // Normally you'd use Task.Factory.StartNew or the thread pool in preference.
9
- // Task.Factory.StartNew is used in Listing 1-17 and the thread pool is described in Listing 1-28.
9
+ This is a bonus example program that shows how to use the TaskFactory object.
10
10
11
- // The example creates a task factory and uses it to create a task that runs until it completes.
12
- // If you press enter before five seconds have elapsed the task is terminated and the program ends.
13
- {
11
+ Normally you'd use Task.Factory.StartNew or the thread pool in preference.
12
+
13
+ Task.Factory.StartNew is used in Listing 1-17 and the thread pool is described in Listing 1-28.
14
+
15
+ The example creates a task factory and uses it to create a task that runs until it completes.
16
+
17
+ If you press enter before five seconds have elapsed the task is terminated and the program ends.
18
+
19
+ */
14
20
class Program
15
21
{
16
22
static void Main ( string [ ] args )
@@ -28,10 +34,10 @@ static void Main(string[] args)
28
34
// Use the factory to create a new Task running do work.
29
35
Task t2 = factory . StartNew ( ( ) => DoWork ( ) ) ;
30
36
31
- Console . WriteLine ( "Press enter to dispose of the task" ) ;
37
+ Console . WriteLine ( "Press enter to dispose of the task. " ) ;
32
38
Console . ReadLine ( ) ;
33
39
34
- Console . WriteLine ( "Disposing of the task" ) ;
40
+ Console . WriteLine ( "Disposing of the task. " ) ;
35
41
36
42
// Cancel the task.
37
43
// Dispose of the task using the cancellation token.
@@ -40,7 +46,7 @@ static void Main(string[] args)
40
46
41
47
static void DoWork ( )
42
48
{
43
- Console . WriteLine ( "Doing work.. " ) ;
49
+ Console . WriteLine ( "Doing work." ) ;
44
50
45
51
int total = 0 ;
46
52
@@ -50,7 +56,7 @@ static void DoWork()
50
56
total += i ;
51
57
}
52
58
53
- Console . WriteLine ( "Result: " + total ) ; // 12497500
59
+ Console . WriteLine ( $ "Result: { total } ." ) ; // 12497500
54
60
}
55
61
}
56
- }
62
+ }
0 commit comments