0

I need my program to wait for about 1ms. To do that I use:

System.Threading.Thread.Sleep(1)

But is about 10 times Slower.

In the following program:

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    for (int i = 0; i < 1000; i++ )
        System.Threading.Thread.Sleep(1);

    Console.WriteLine(stopWatch.ElapsedMilliseconds);

I get as output: 15110.

Is there a way to stop for just 1ms?

UPDATE 1: Why I need to do this

In the company where I work, there's a machine (CNC Plasma) controlled by a PC. Somebody has protected the config area by a password and nobody remembers the password. Since the password can only be entered by mouse, I'm am writting a force brute program to get that password.

The problem is that I need to wait for about 1ms between each entered digit.

UPDATE 2: There's a similar question but is not solved :(

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Jaime Oro
  • 9,899
  • 8
  • 31
  • 39
  • 6
    Why are you wanting to block your program for exactly 1ms? – Dai Nov 21 '14 at 10:07
  • 1
    [Related](http://stackoverflow.com/q/8742/1997232). – Sinatr Nov 21 '14 at 10:07
  • 2
    ***Remember** that the `for` loop will also slow it down, and so will the `Console.WriteLine` and the timer itself. It *could* be stopping the program for 1ms, but your extra `for` loop and timers slows it down. I find that writing output to the screen slows down everything most, perhaps assign the value to a variable before the `Console.WriteLine` and then output that variable. – AStopher Nov 21 '14 at 10:07
  • 9
    Classic [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Sriram Sakthivel Nov 21 '14 at 10:08
  • @cibermonkey True, but it can't slow 14secs in this 1000 iterations loop. – Jaime Oro Nov 21 '14 at 10:08
  • 1
    So to get more accurate readings unroll the `for` loop. – Grx70 Nov 21 '14 at 10:09
  • 1
    As @SriramSakthivel said this is a classic XY Problem - why do you *think* you need to halt your program for exactly 1ms? – Jamiec Nov 21 '14 at 10:10
  • Windows is not a real-time OS. It will not work that way. You could loop with checking the StopWatch, but there will be a high CPU load. – JeffRSon Nov 21 '14 at 10:10
  • 1
    The real question here would be why you want to do this. If we knew that we might be able to help with your actual problem. Please let us know what your problem is rather than asking about your solution. – David Heffernan Nov 21 '14 at 10:11
  • @Dai I don't need to stop 1ms exactly but something closed. System.Threading.Thread.Sleep(1) stops for 15ms which is too much. – Jaime Oro Nov 21 '14 at 10:11
  • 1
    There is [`timeBeginPeriod`](http://msdn.microsoft.com/en-us/library/dd757624.aspx), but make sure to understand its downsides. – CodesInChaos Nov 21 '14 at 10:11
  • @JaimeOro We all think you DON'T need to stop for 1ms, can you explain why you think you do, what problem do you think you will solve by halting thread execution for 1ms. – Ben Robinson Nov 21 '14 at 10:14
  • 1 ms is pretty close to 15 ms to me :) – L-Four Nov 21 '14 at 10:15
  • 2
    Seems the "real" duplicate is here: http://stackoverflow.com/questions/9518106/winapi-sleep-function-call-sleeps-for-longer-than-expected – JeffRSon Nov 21 '14 at 10:19
  • Regarding update, use a busy loop with a high perf timer – David Heffernan Nov 21 '14 at 10:19
  • 1
    @DavidHeffernan: Yes, see my comment above. – JeffRSon Nov 21 '14 at 10:21
  • @Sriram Sakthivel no it's not. The question is "Is there a way to stop for just 1ms?" and my not-working proposal is presented. – Jaime Oro Nov 21 '14 at 10:27
  • @JeffRSon As far as I'm aware, questions cannot be marked as a duplicate if it uses a different programming language to the 'duplicate'. – AStopher Nov 21 '14 at 10:35

1 Answers1

7

I think the problem is in your understanding of the Thread.Sleep.

It doesn't wait for exactly 1 millisecond. It will wait at least 1 millisecond.

According to MSDN:

The thread will not be scheduled for execution by the operating system for the amount of time specified.

When the OS picks up the thread again, more than 1 millisecond may have passed. The difference will be less noticeable when you increase the time to wait. If you iterate and wait 1ms, the difference may be relatively very large.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325