forked from oferzad/EventsAndThreadsLecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (128 loc) · 5.41 KB
/
Program.cs
File metadata and controls
157 lines (128 loc) · 5.41 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
152
153
154
155
156
157
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Threading;
using System.Net.Http.Headers;
namespace CoreCollectionsAsync
{
class Program
{
static async Task DemoAsync()
{
Console.WriteLine("Start Demo...");
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Before...{i} {Environment.CurrentManagedThreadId}");
}
Task.Run(() => GetRandomAsync());
Console.WriteLine($"After... ");
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"After...{i} {Environment.CurrentManagedThreadId}");
}
Console.ReadKey();
}
static async Task<int> GetRandomAsync()
{
await Task.Delay(0);
Random r = new Random();
for (int i = 0; i < 100; i++)
{
Console.WriteLine($"GetRandomAsync...{i} {Environment.CurrentManagedThreadId}");
}
return r.Next(0, 100);
}
static async Task<int> DoSomethingIntAsync(int sec)
{
Console.WriteLine($"Lets Wait for {sec} seconds... ThreadID:{Environment.CurrentManagedThreadId}");
await Task.Delay(1000 * sec);
Console.WriteLine($"Done with waiting! ThreadID:{Environment.CurrentManagedThreadId}");
return sec;
}
static async Task Start3()
{
Console.WriteLine($"Before running DoSomethingIntAsync for 2 sec ThreadID:{Environment.CurrentManagedThreadId}");
int x = await DoSomethingIntAsync(2);
Console.WriteLine($"x = {x}");
Console.WriteLine($"After running DoSomethingIntAsync! ThreadID:{Environment.CurrentManagedThreadId}");
}
//********************************************************************
static async Task DoSomethingAsync(int sec)
{
Console.WriteLine($"Lets Wait for {sec} seconds... ThreadID:{Environment.CurrentManagedThreadId}");
await Task.Delay(1000 * sec);
Console.WriteLine($"Done with waiting! ThreadID:{Environment.CurrentManagedThreadId}");
}
static void Start2()
{
Console.WriteLine($"Before running DoSomethingAsync for 2 sec ThreadID:{Environment.CurrentManagedThreadId}");
Task t = DoSomethingAsync(2);
Console.WriteLine($"After running DoSomethingAsync! ThreadID:{Environment.CurrentManagedThreadId}");
t.Wait();
}
//********************************************************************
static void DoSomething(int sec)
{
Console.WriteLine($"Lets Wait for {sec} seconds... ThreadID:{Environment.CurrentManagedThreadId}");
Task t = Task.Delay(1000 * sec);
t.Wait();
Console.WriteLine($"Done with waiting! ThreadID:{Environment.CurrentManagedThreadId}");
}
static void Start()
{
Console.WriteLine($"Before running DoSomething for 2 sec ThreadID:{Environment.CurrentManagedThreadId}");
DoSomething(2);
Console.WriteLine($"After running DoSomething! ThreadID:{Environment.CurrentManagedThreadId}");
Console.ReadKey();
}
//********************************************************************
static void GoToSleep()
{
Console.WriteLine($"Going to sleep... ThreadID:{Environment.CurrentManagedThreadId}");
Thread.Sleep(2000);
Console.WriteLine($"Just woke up! ThreadID:{Environment.CurrentManagedThreadId}");
}
static void StartGoToSleep()
{
Console.WriteLine($"Running go to sleep ThreadID:{Environment.CurrentManagedThreadId}");
Task t = Task.Run(GoToSleep);
Console.WriteLine($"After running go to sleep ThreadID:{Environment.CurrentManagedThreadId}");
t.Wait();
}
static void CarSut()
{
Console.WriteLine("nani?");
Console.WriteLine("eeeeeeeeeeeeee");
}
static void Main(string[] args)
{
for (int i = 0; i < 20; i++)
{
ElectricCar coolcar = new ElectricCar(i);
coolcar.OnCarShutDown += CarSut;
Thread car = new Thread(coolcar.StartEngine);
car.Start();
}
Thread.Sleep(99999);
//DemoAsync().Wait();
//1. Prepare Omlette with no progress bar
//DelegateAndEventsDemo.RunDemo_1();
//2. Prepare Omlette with Progress and Finish Events
//DelegateAndEventsDemo.RunDemo_2();
//3. Prepare a full breakfast! no threads
//BreakfastWIthThreads.MakeBreakfastDemo_1();
//4. Prepare a full breakfast using async methods!
//BreakfastWIthThreads.MakeBreakfastDemoAsync_4().Wait();
//5. Prepare a full breakfast ysing async and a Any method + list of tasks
//BreakfastWIthThreads.MakeBreakfastDemoAsync_5().Wait();
//6. Critical section
//AccountTest.StartDemo1();
//7. Show a dead lock example
//DeadLock.StartTest();
//8. Solution of events and threads exercise
//EventsExercise.Start2();
}
}
}