Hello All,
I'm working on a program that controls Pan-tilt cameras through a serial port. The commands are sent to the cameras in two ways. Either immediately or delayed up to five minutes. My current approach sends the commands to a queue collection that's checked with a timer event every second. From what I understand each of the objects (timer and port) have their own thread.
This code actually works but I think it's a very poor approach. I would also like to find a better way to remove the sent commands from the _queue collection.
I'm hoping someone could give me some advice or direction on a better approach to this.
Thanks for your help!
I'm working on a program that controls Pan-tilt cameras through a serial port. The commands are sent to the cameras in two ways. Either immediately or delayed up to five minutes. My current approach sends the commands to a queue collection that's checked with a timer event every second. From what I understand each of the objects (timer and port) have their own thread.
This code actually works but I think it's a very poor approach. I would also like to find a better way to remove the sent commands from the _queue collection.
VB.NET:
Private Sub queueTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _queueTimer.Elapsed
Dim cmd As Command
Dim al As New ArrayList
For Each cmd In _queue
If cmd.StartTime <= Date.Now Then
SyncLock _port
'send the command
SendCommand(cmd)
'add commands to an arraylist for removal
al.Add(cmd)
Debug.Print(cmd.FriendlyMessage)
End SyncLock
End If
Next
'Remove all completed commands
Dim i As Integer
For i = 0 To al.Count - 1
_queue.Remove(al(i))
Debug.Print(al(i).ToString)
Next
End Sub
Public Sub SendCommand(ByVal cmd As Command)
'See if the command should be sent now or put in a queue
If cmd.StartTime <= Date.Now Then
SyncLock _port
_port.WriteLine(cmd.GetCommandString)
End SyncLock
'TODO Log the event
Else : _queue.Add(cmd)
End If
End Sub
I'm hoping someone could give me some advice or direction on a better approach to this.
Thanks for your help!