This repository was archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (44 loc) · 1.68 KB
/
Program.cs
File metadata and controls
57 lines (44 loc) · 1.68 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
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace CSharpAsyncExample
{
public class Kitchen
{
private static Stopwatch Stopwatch { get; set; }
static void Main(string[] args)
{
Stopwatch = new Stopwatch();
Stopwatch.Start();
Console.WriteLine($"[Kitchen] Started making breakfast - Synchronous Chef");
MakeBreakfastWithSynchronousChef();
Console.WriteLine($"[Kitchen] Breakfast done in '{Stopwatch.Elapsed.Seconds}'s '{Stopwatch.Elapsed.Milliseconds}'ms");
Stopwatch.Restart();
Console.WriteLine("---");
Console.WriteLine($"[Kitchen] Started making breakfast - Asynchronous Chef");
MakeBreakfastWithAsynchronousChef().Wait();
Console.WriteLine($"[Kitchen] Breakfast done in '{Stopwatch.Elapsed.Seconds}'s '{Stopwatch.Elapsed.Milliseconds}'ms");
}
static void MakeBreakfastWithSynchronousChef()
{
var chef = new SynchronousChef();
chef.MakeBreakfast();
}
static async Task MakeBreakfastWithAsynchronousChef()
{
var chef = new AsynchronousChef();
await chef.MakeBreakfast();
}
static async Task MakeBreakfastAndDisturbChef()
{
var chef = new AsynchronousChef();
var breakfastTask = chef.MakeBreakfast();
Task.Delay(1500).Wait();
Console.WriteLine($"[Kitchen] a joke from chef: '{chef.TellAJoke().Result}'");
Task.Delay(9000).Wait();
chef.PourJuice().Wait();
await breakfastTask;
}
}
}