43
1
using System;
2
using System.Threading;
3
using System.Threading.Tasks;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
Console.WriteLine("Hello from main thread {0}", Thread.CurrentThread.ManagedThreadId);
10
11
//Implicitly
12
UsingParallelInvoke();
13
14
//Explicitly
15
UsingTaskRun();
16
UsingTaskNew();
17
UsingTaskFactory();
18
}
19
20
protected static void UsingParallelInvoke() {
21
Console.WriteLine("Using parallel invoke");
22
Parallel.Invoke(() => Console.WriteLine("Hello from Task {0}", Thread.CurrentThread.ManagedThreadId));
23
}
24
25
protected static void UsingTaskRun() {
26
Console.WriteLine("Using task run static method");
27
Task taskA = Task.Run(() => Console.WriteLine("Hello from Task {0}", Thread.CurrentThread.ManagedThreadId));
28
taskA.Wait();
29
}
30
31
protected static void UsingTaskNew() {
32
Console.WriteLine("Using new to create task");
33
Task taskA = new Task(() => Console.WriteLine("Hello from Task {0}", Thread.CurrentThread.ManagedThreadId));
34
taskA.Start();
35
taskA.Wait();
36
}
37
38
protected static void UsingTaskFactory() {
39
Console.WriteLine("Using task factory");
40
Task taskA = Task.Factory.StartNew(() => Console.WriteLine("Hello from Task {0}", Thread.CurrentThread.ManagedThreadId));
41
taskA.Wait();
42
}
43
}
Cached Result
directory not found exception handled
Tasks Exception: ArgumentNullException Value cannot be null. (Parameter 'path')
Tasks Exception: ArgumentNullException Value cannot be null. (Parameter 'path')