-
Notifications
You must be signed in to change notification settings - Fork 0
/
DelayQueue.vb
144 lines (104 loc) · 3.38 KB
/
DelayQueue.vb
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
Imports System.Collections
Imports System.Windows.Forms
Public Class DelayQueue
Private Class DelayedCommand
Public Property Delay As CasparCG.Retard
Public Property Command As String
Public Sub New(delay As Integer, command As String)
Me.Delay.Amount = delay
Me.Command = command
End Sub
End Class
Private _Caspar As CasparCG = Nothing
Private _queue As List(Of DelayedCommand) = New List(Of DelayedCommand)
Private _currentDelay As Integer = 0
Private _nextCommand As String = ""
Private WithEvents _timer As Timer = New Timer()
Public WriteOnly Property Caspar As CasparCG
Set(value As CasparCG)
_Caspar = value
End Set
End Property
Public Sub Add(delay As Integer, command As String)
If _currentDelay > 0 Then
delay -= _currentDelay
If delay < 0 Then
delay = 0
End If
End If
If _queue.Count = 0 Then
_queue.Add(New DelayedCommand(delay, command))
ElseIf _queue.Count = 1 Then
If _queue(0).Delay.Amount >= delay Then
_queue.Insert(0, New DelayedCommand(delay, command))
Else
_queue.Add(New DelayedCommand(delay, command))
End If
Else
If _queue(0).Delay.Amount >= delay Then
_queue.Insert(0, New DelayedCommand(delay, command))
Else
Dim i As Integer = 0
Do
Dim dc1 As DelayedCommand = _queue(i)
Dim dc2 As DelayedCommand = Nothing
If i < _queue.Count - 1 Then
dc2 = _queue(i + 1)
End If
If delay >= dc1.Delay.Amount Then
If dc2 IsNot Nothing Then
If delay < dc2.Delay.Amount Then
_queue.Insert(i + 1, New DelayedCommand(delay, command))
Exit Do
End If
Else
_queue.Add(New DelayedCommand(delay, command))
Exit Do
End If
Else
If dc2 Is Nothing Then
_queue.Add(New DelayedCommand(delay, command))
Exit Do
End If
End If
i += 1
If i > _queue.Count - 1 Then
Exit Do
End If
Loop
End If
End If
StartTimer()
End Sub
Private Sub StartTimer()
If _queue.Count > 0 Then
If Not _timer.Enabled Then
Dim top As DelayedCommand = _queue(0)
_currentDelay = top.Delay.Amount
_nextCommand = top.Command
_queue.RemoveAt(0)
For Each dc As DelayedCommand In _queue
dc.Delay.Amount -= _currentDelay
Next
If _currentDelay = 0 Then
_Caspar.Execute(_nextCommand)
StartTimer()
Else
_timer.Interval = _currentDelay
_timer.Start()
End If
End If
End If
End Sub
Private Sub _timer_Tick(sender As Object, e As EventArgs) Handles _timer.Tick
_timer.Stop()
_currentDelay = 0
_Caspar.Execute(_nextCommand)
StartTimer()
End Sub
Public Sub New()
End Sub
Public Sub New(Caspar As CasparCG)
_Caspar = Caspar
End Sub
End Class