5

I have an app with 2 threads (now), but it seems that function Thread.Sleep() doesn't work very good. It sleeps threads but it takes much more time (for example- I want to sleep it for 5ms and it sleeps for 0,3s or more). Here is code:

int vlakien = 2;
Thread[] vlakna; 
vlakna = new Thread[vlakien];

for (int i = 0; i < vlakien; i++) 
{ try { vlakna[i] = new Thread(new ThreadStart(utok)); vlakna[i].Start(); } }

private void utok()
{
  //some code
  Thread.Sleep(5);
  //some code
}

Also I tried to sleep it with Stopwatch in the function utok and it also takes more time:

Stopwatch SW = new Stopwatch(); SW.Start();
while(SW.ElapsedMilliseconds < 5000) ;

Please help.

Cody Gray
  • 230,875
  • 49
  • 477
  • 553
matej148
  • 135
  • 1
  • 13
  • 2
    Yes, `Thread.Sleep` is not guaranteed to sleep for exactly the specified amount of time. And using it in general is a sign that your application is poorly-designed. It's not clear what problem you're trying to use it to solve. Consider updating your question with a discussion of the *real* problem, rather than your failed solution. – Cody Gray Jan 13 '12 at 01:27
  • I am sending a huge amount of packets with sharppcap and packetdotnet (DoS attack simulation [diploma work]) and I want to simulate it with small delays between packets (without delay/5ms delay/10ms delay...). – matej148 Jan 13 '12 at 01:33

5 Answers5

9

15ms is the thread time slice on windows ( you can actually mess with windows and change that.... not at all recommended). Things can give their time slices up early, but anything could take their full time slice.

So its really hard to get any better than that, in fact, realistically 20 or 30 ms is more likely. I used to do real time processing which had a hard real time limit of 50ms. That worked well on windows if you obeyed certain rules ( it was in C++)

Keith Nicholas
  • 42,517
  • 15
  • 87
  • 149
  • Is there any article in MSDN to provide proof for this? – PawanS Sep 25 '13 at 10:39
  • This is for Task.Delay but probably same goes for Thread.Sleep... https://msdn.microsoft.com/en-us/library/hh194845(v=vs.110).aspx – Binier Sep 07 '17 at 05:32
5

As others have indicated, the default resolution of Sleep is 10 or 15 milliseconds, depending on the edition of Windows.

However, you can reprogram the timer to use a 1 millisecond resolution by issuing a

timeBeginPeriod(1);
timeEndPeriod(1);

where

[DllImport(WINMM)]
internal static extern uint timeBeginPeriod(uint period);   

We do this in our serial communications services where being able to accurately space out sends in time is important. Some people are reluctant to do this because it causes Windows to do other things that are based off of the timer more frequently as well. In reality this has caused no discernible issues for us, and we have hundreds of installs each with hundreds of serial devices connected.

Matt
  • 724
  • 1
  • 10
  • 27
  • It wouldn't be a problem on a dedicated machine, but I'd hardly recommend it for general use. And if your *installer* ever did that to a machine of mine, I'd probably go postal. More to the point, this still doesn't ensure that the timer actually uses a 1 millisecond resolution. Windows is still not a real-time operating system. – Cody Gray Jan 13 '12 at 09:07
  • These are dedicated machines. And you are of course correct that this does not alter the fact that Windows is not a real-time OS. – 500 - Internal Server Error Jan 13 '12 at 18:29
  • 1
    It looks like you're supposed to call `timeEndPeriod` as well - "You must match each call to `timeBeginPeriod` with a call to `timeEndPeriod`, specifying the same minimum resolution in both calls." https://msdn.microsoft.com/en-us/library/windows/desktop/dd757624(v=vs.85).aspx – Matt Mar 13 '18 at 10:19
1

Sleep in Microsoft C# guarantees a minimum amount of time. It sleeping less than 5 milliseconds is definitely a problem. Also Stopwatch may not be very precise measurement, try the high precision media timers.

nmjohn
  • 1,432
  • 7
  • 16
  • 3
    I disagree Stopwatch will provide high precision timing just as the media timers. As far as I know it's the same timer that media timers use which is the QueryPerformanceCounter. I know of no other hi resolution timer on a standard windows PC. – galford13x May 21 '13 at 16:41
0

It is affected by resolution of the system clock. It is around 15ms...you cannot go below resolution of the system clock. Take a look at this link (it is C++ but you will get idea about timer resolutions).

Aleksandar Vucetic
  • 14,327
  • 8
  • 50
  • 54
0

The parameter passed into Thread.Sleep is a minimum time to sleep, not an exact time.

brianestey
  • 7,862
  • 5
  • 32
  • 47