Threading - Add a 1 second delay between calls
Hi,
I am sending commands to some server via an UdpClient. However, I need to make sure that all commands send are separated by a 1 second delay. If there is no delay, two commands send within this 1 second timespan will not be received both; only the first or the second command will get through.
The commands are sent in a background thread at the moment, because it might take some time before the command is sent. I keep looping until the command has been sent, like this:
vb.net Code:
Public Sub Send(command As String)
Dim bytes = Me.GetCommandBytes(command)
Dim t As New Thread(ThreadSend)
t.Start(bytes)
End Sub
Private Sub ThreadSend(commandBytes As Object)
Dim bytes = DirectCast(commandBytes, Byte())
Dim sent = False
While Not sent
Me.Client.Send(bytes, bytes.Length, _IpEndPoint)
Thread.Sleep(200)
If Me.Client.Available > 0 Then
sent = True
End If
End While
End Sub
This does not keep into mind the 1 second delay though. When I send four commands consecutively, like:
Code:
Send("test1")
Send("test2")
Send("test3")
Send("test4")
The results seem a little random, but I never get them all. Sometimes just 1, sometimes 1 and 3, sometimes 2 and 3, sometimes even 4 and then 1 :confused:
I have reason to believe that the 1 second delay will fix this (if not, well, then I'm clueless, but I'm sure you are as well so there's no point in asking).
I'm not sure how to build this delay however. Simply adding a Thread.Sleep(1000) in the ThreadSend method won't work of course; the four commands sent consecutively will all wait one second and then still be sent (nearly) at the same time. There needs to be a delay between commands, not before commands.
I am thinking I need some kind of Queue which I fill with the commands in the Send method. Then another thread is constantly looping through this Queue, in one second intervals, and sending the first command (if one is available).
However, I have a feeling something like this should be easier. I have heard about the ThreadPool before, which sounds like it might do what I want already, but I have no clue how to use it. The thing is: the ThreadPool seems to fire off as many threads as it can before it queues up new threads; and it handles those new threads as soon as they are available, which is not what I want at all.
Any ideas?
Re: Threading - Add a 1 second delay between calls
I would use a queue - it's something that this class was designed for:
vb Code:
Public Class Form1
Private msgQueue As New Queue(Of String)
Private qMgr As Threading.Thread
Private Sub Send(ByVal text As String)
msgQueue.Enqueue(text)
End Sub
Private Sub QueueManager()
Do
Threading.Thread.Sleep(1000)
If msgQueue.Count > 0 Then
Dim str As String = msgQueue.Dequeue()
'send string here
End If
Loop
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
qMgr = New Threading.Thread(AddressOf QueueManager)
qMgr.Start()
End Sub
End Class
Re: Threading - Add a 1 second delay between calls
Yeah, that is similar to what I'm going with now. Mine is a little more complicated, I can stop the queue reading when I want to (and it's stopped neatly when the application closes of course), but I don't have that much experience with threading and I'm afraid I might run into cross-threading errors or something like that. I was just wondering if there was something built in (ThreadPool for example) which handles everything gracefully for me.
Re: Threading - Add a 1 second delay between calls
Is there some delay on the receiving end?
Re: Threading - Add a 1 second delay between calls
Quote:
Originally Posted by
NickThissen
I'm afraid I might run into cross-threading errors or something like that. I was just wondering if there was something built in (ThreadPool for example) which handles everything gracefully for me.
Unless you overcomplicated this example, there shouldn't be any issues.
Well then, if you're worried, you can synclock/unlock your queue while it is being modified.
As for 'handles everything gracefully' - I don't think they at Microsoft did that much. You have to always cross your fingers when you do into multithreading.
One queue and two threads shouldn't cause any problems though.