-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask19.psm1
More file actions
85 lines (75 loc) · 2.88 KB
/
task19.psm1
File metadata and controls
85 lines (75 loc) · 2.88 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
<#
Управление процессами (набор функций).
В режиме диалога формируется ассоциативный массив: имя_процесса, время завершения.
При запросе состояния массива должны выдаваться сообщения о завершившихся процессах
(относительно текущего времени запроса) и такие процессы должны быть уже удалены
из ассоциативного массива.
#>
class ProcessDispatcher {
hidden [hashtable] $Processes = @{}
hidden [datetime] $LastCheck = $(Get-Date)
[void] AddProcess([string] $ProcessName, [datetime] $EndingTime) {
if ($EndingTime -le $(Get-Date)) {
throw "EndingTime must be greater than current time"
}
Write-Host "Process $ProcessName has started at $EndingTime"
$this.Processes.Add($ProcessName, $EndingTime)
}
[hashtable] GetProcesses() {
$this.UpdateState()
return $this.Processes
}
[void] UpdateState() {
$CurrentMoment = Get-Date
$ActiveProcesses = @{}
foreach ($ProcessName in $this.Processes.Keys) {
$ProcessEndingTime = $this.Processes[$ProcessName]
if ($ProcessEndingTime -le $CurrentMoment) {
Write-Host "Process $ProcessName has terminated at $ProcessEndingTime"
}
else {
$ActiveProcesses.Add($ProcessName, $ProcessEndingTime)
}
}
$this.Processes = $ActiveProcesses
}
}
function Read-UserCommand() {
return Read-Host $(
"Enter your command (add to add process, update to " +
"update dispatcher state, get - to get active processes " +
"quit - to end the dialog)"
)
}
function Start-ProcessDispatcherDialog {
Write-Host "Dialog was started"
$ProcessDispatcherInstance = [ProcessDispatcher]::new()
$Input = Read-UserCommand
while ($Input -ne "quit") {
switch ($Input) {
"add" {
try {
$ProcessName = Read-Host "Process Name"
$EndingTime = Read-Host "Ending Time"
$ProcessDispatcherInstance.AddProcess($ProcessName, $EndingTime)
}
catch {
Write-Host $_.Exception.Message
}
}
"update" {
$ProcessDispatcherInstance.UpdateState()
}
"get" {
$ProcessDispatcherInstance.GetProcesses()
}
default {
Write-Host "Unknown command"
}
}
$Input = Read-UserCommand
}
Write-Host "Dialog was ended"
}
Export-ModuleMember Start-ProcessDispatcherDialog
Export-ModuleMember ProcessDispatcher