0

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
Aditya369
  • 545
  • 1
  • 7
  • 27

4 Answers4

1

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 )

rskar
  • 4,607
  • 25
  • 21
  • Okay.. but cannot we sleep() (or any other alternative fun()) to sleep of the order of microseconds? In VB, am I helpless with this problem? – Aditya369 Sep 20 '11 at 14:33
  • In Windows, you will need to experiment with Timers (see http://msdn.microsoft.com/en-us/library/ms644900(v=vs.85).aspx ). You can also google "microsecond sleep in windows" for other ideas. – rskar Sep 20 '11 at 16:18
  • Can we use this way? Thread.Sleep(New TimeSpan(delay * TimeSpan.TicksPerSecond)); – Aditya369 Sep 20 '11 at 16:50
  • sleep function may have milli second resolution, but underlying system timers typically have longer resolutions. – David Heffernan Sep 20 '11 at 16:56
0

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

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?

Hand-E-Food
  • 12,368
  • 8
  • 45
  • 80
  • @Hand-E-Food 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.. – Aditya369 Sep 21 '11 at 05:06
0

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
Aditya369
  • 545
  • 1
  • 7
  • 27
  • I think this is approx. working for me... which is fine for my requirement. Any clever/more sophisticated ways.. pls do suggest. – Aditya369 Sep 21 '11 at 14:42
  • DoEvents isn't going to be compatible with microsecond waits. The rest of what you implemented is what I said in my answer. – David Heffernan Sep 22 '11 at 22:29