Search code examples
vb.netthread-sleeptimedelay

VB .net could not sleep() for very very short durations


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?


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 )