-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (93 loc) · 2.72 KB
/
Program.cs
File metadata and controls
107 lines (93 loc) · 2.72 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
PrintHelp();
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
List<string> list4 = new List<string>();
CancellationTokenSource cts = new CancellationTokenSource();
void AddStringsToList(List<string> list)
{
while (true)
{
if (cts.Token.IsCancellationRequested)
break;
list.Add($"String added at {DateTime.Now}");
}
}
void ClearList(List<string> list)
{
list.Clear();
list.TrimExcess();
}
void PrintHelp()
{
Console.WriteLine("=== Application Help ===");
Console.WriteLine("Available commands:");
Console.WriteLine("start - Start adding strings to the lists.");
Console.WriteLine("stop - Stop adding strings to the lists and exit the application.");
Console.WriteLine("continue - The tasks are already running.");
Console.WriteLine("clear - Clear all the lists.");
Console.WriteLine("gc - Call the garbage collector.");
Console.WriteLine("help - Display this help message.");
Console.WriteLine("========================");
}
var isStarted = false;
var tasks = new List<Task>();
while (true)
{
string? input = Console.ReadLine();
if (input == "start")
{
if (isStarted)
{
Console.WriteLine("The tasks are already running.");
continue;
}
cts = new CancellationTokenSource();
tasks.Add(Task.Run(() => AddStringsToList(list1), cts.Token));
tasks.Add(Task.Run(() => AddStringsToList(list2), cts.Token));
tasks.Add(Task.Run(() => AddStringsToList(list3), cts.Token));
tasks.Add(Task.Run(() => AddStringsToList(list4), cts.Token));
isStarted = true;
Console.WriteLine("Tasks started.");
}
else if (input == "stop")
{
if (!isStarted)
{
Console.WriteLine("The tasks are not running.");
continue;
}
cts.Cancel();
await Task.WhenAll(tasks);
isStarted = false;
Console.WriteLine("Tasks stopped.");
}
else if (input == "clear")
{
if (isStarted)
{
Console.WriteLine("Stop tasks before clearing.");
continue;
}
ClearList(list1);
ClearList(list2);
ClearList(list3);
ClearList(list4);
Console.WriteLine("Lists cleared.");
}
else if (input == "gc")
{
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Garbage collector called.");
}
else if (input == "exit")
{
break;
}
else if (input == "help")
{
PrintHelp();
}
}
Console.WriteLine("Testing has been finished. Press any key to exit ...");