-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (117 loc) · 3.3 KB
/
Program.cs
File metadata and controls
151 lines (117 loc) · 3.3 KB
1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Threading;
using System.Threading.Tasks;
namespace multithreading
{
class Program
{
static async Task<int> Main(string[] args)
{
SimpleThreadExample simpleThreadExample = new SimpleThreadExample();
Task t1 = simpleThreadExample.DoSometh("t1");
Task t2 = simpleThreadExample.DoSometh("t2");
Task t3 = simpleThreadExample.DoSometh("t3");
Task t4 = simpleThreadExample.DoSometh("t4");
Task.Run(() => t1);
//Task.Run(() => t2);
Task.Run(() => t3);
Task.Run(() => t4);
Task.WaitAll(t1);
await t2;
return 1;
}
}
}
public class SimpleThreadExample
{
public void StartMultipleThread()
{
DateTime startTime = DateTime.Now;
Thread t1 = new Thread(() =>
{
int numberOfSeconds = 0;
while (numberOfSeconds < 5)
{
Thread.Sleep(1000);
numberOfSeconds++;
}
Console.WriteLine("I ran for 5 seconds");
});
Thread t2 = new Thread(() =>
{
int numberOfSeconds = 0;
while (numberOfSeconds < 8)
{
Thread.Sleep(1000);
numberOfSeconds++;
}
Console.WriteLine("I ran for 8 seconds");
});
//parameterized thread
Thread t3 = new Thread(p =>
{
int numberOfSeconds = 0;
while (numberOfSeconds < Convert.ToInt32(p))
{
Thread.Sleep(1000);
numberOfSeconds++;
}
Console.WriteLine("I ran for {0} seconds", numberOfSeconds);
});
t1.Start();
t2.Start();
//passing parameter to parameterized thread
t3.Start(20);
//wait for t1 to finish
t1.Join();
//wait for t2 to finish
t2.Join();
//wait for t3 to finish
t3.Join();
Console.WriteLine("All Threads Exited in {0} secods", (DateTime.Now - startTime).TotalSeconds);
}
public async Task DoSometh(string name)
{
Console.WriteLine($"entering task {name} {Task.CurrentId}" );
await Task.Delay(10000);
Console.WriteLine($"exiting do someth {name} {Task.CurrentId}");
}
public async void DoVoid(string name)
{
Console.WriteLine($"entering task {Task.CurrentId}");
await Task.Delay(10000);
Console.WriteLine($"exiting do someth {Task.CurrentId}");
}
}
public class DestroyThreadExample
{
public bool IsCancelled { get; set; }
public Thread MyThread { get; set; }
public void StartThread()
{
MyThread = new Thread(() =>
{
int numberOfSeconds = 0;
while (numberOfSeconds < 8)
{
if (IsCancelled == false)
{
break;
}
Thread.Sleep(1000);
numberOfSeconds++;
}
Console.WriteLine("I ran for {0} seconds", numberOfSeconds);
});
}
//Destroy thread
public void Abort()
{
MyThread.Abort();
}
//Graceful exit
public void GracefulAbort()
{
IsCancelled = true;
}
}