Question

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:

  1. I have to keep sending packets after a short nap of above range. So, I could not specify, for eg., to sleep for 0.001 milliseconds at the client side.
  2. My requirement is for the client side of CS app, which reads a file and keeps sending small packets at regular or irregular interval of sleeps to the Server. The Server, later, captures the packets and processes it at its own rate.

How to achieve this?

Was it helpful?

Solution

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 )

OTHER TIPS

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.

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?

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top