Reputation: 566
I would like to sleep for packetSize/Bandwidth amount of duration for a network application in VB.
This value varies from "10^-3 - 10^-6" seconds.
When I use the Sleep() func, I think it is not sleeping for the duration if it is less than unity (< 1 sec ~~ 0 sec??).
Edited:
How to achieve this?
Upvotes: 0
Views: 957
Reputation: 566
I am just studying about the StopWatch concept in VB... Will the following code block work?
Private Sub MyWaiter(ByVal delay As Double)
Dim sw As Stopwatch = New Stopwatch()
sw.Start()
Do While sw.ElapsedTicks < delay * TimeSpan.TicksPerSecond
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
Upvotes: 0
Reputation: 12794
I suspect you're overthinking the problem. Network commuications have a buffer because they can't realistically expect the application to be ready and waiting for every bit that is transmitted. I'd suggest reading as much data as is available, sleep briefly and repeat.
Do Until all data is read
While buffer contains data
read data
Wend
' Let other threads have a go, but come back as soon as possible.
Thread.Sleep(0)
Loop
Does this suit your purpose?
Upvotes: 0
Reputation: 612954
There aren't any system wait functions that timeout at micro-second resolution. You would need a busy loop and a high resolution timer. Of course, the system may preempt your thread anyway so you can forget about realtime guarantees on Windows.
Upvotes: 0
Reputation: 4657
The resolution of Sleep is to the millisecond - that's 10^-3. (See http://msdn.microsoft.com/en-us/library/d00bd51t.aspx and http://msdn.microsoft.com/en-us/library/274eh01d.aspx ). One second is 1000 milliseconds; that's Sleep(1000).
You cannot use Sleep() for values less than 10^-3 seconds. I have used Sleep() with values of 62, 125, and 250 milliseconds successfully.
You can experiment with System.Diagnostics.Stopwatch; maybe a Wait or Pause function can be built from that. (See http://msdn.microsoft.com/en-us/library/ebf7z0sw.aspx )
Upvotes: 1