4

I'm trying simulate a key down and key up action.

For example: 2638 millseconds.

SendMessage(hWnd, WM_KEYDOWN, keyCode, 0);
Sleep(2638);
SendMessage(hWnd, WM_KEYUP, keyCode, 0);

How would you know if it really worked?

user963241
  • 6,758
  • 19
  • 65
  • 93
  • Thread switch frequency is about 40 milliseconds. You're thread usually executes at least every 3 thread switch ticks. So you're talking about a 120ms seconds percision worse case. This is measured on an XP machine P4 HT a couple of years ago and without changing the NT_Thread switch frequency. – CodingBarfield Feb 01 '11 at 13:01

4 Answers4

6

You wouldn't with this code, since accurately measuring the time that code takes to execute is a difficult task.

To get to the question posed by your question title (you should really ask one question at a time...) the accuracy of said functions is dictated by the operating system. On Linux, the system clock granularity is 10ms, so timed process suspension via nanosleep() is only guaranteed to be accurate to 10ms, and even then it's not guaranteed to sleep for exactly the time you specify. (See below.)

On Windows, the clock granularity can be changed to accommodate power management needs (e.g. decrease the granularity to conserve battery power). See MSDN's documentation on the Sleep function.

Note that with Sleep()/nanosleep(), the OS only guarantees that the process suspension will last for at least as long as you specify. The execution of other processes can always delay resumption of your process.

Therefore, the key-up event sent by your code above will be sent at least 2.638 seconds later than the key-down event, and not a millisecond sooner. But it would be possible for the event to be sent 2.7, 2.8, or even 3 seconds later. (Or much later if a realtime process grabbed hold of the CPU and didn't relinquish control for some time.)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 6
    "The OS only guarantees that the process suspension will last for at least as long as you specify." On Windows, this isn't actually true. From the MSDN link you posted: "the thread may sleep for less than the specified length of time". – user200783 Nov 08 '10 at 22:58
  • Good point, somehow I missed that... I would assume that the thread may sleep for not less than the requested time minus the current clock granularity, but I can't find any documentation on this. – cdhowie Nov 08 '10 at 23:09
  • 1
    Recent (last couple of years) Linux kernels with the NOHZ option do not have any minimum granularity, other than that imposed by the hardware itself. – caf Nov 08 '10 at 23:45
  • I believe that's true - I think it's what the linked page is trying to say by: "If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on." However that quote is misleading, since it implies there is an *upper* bound on the suspension time. – user200783 Nov 10 '10 at 20:34
  • 1
    Right. So it would probably be correct to say that if dwMilliseconds is greater than N ticks but less than M, the wait will be at least N ticks. – cdhowie Nov 10 '10 at 20:37
1

Sleep works in terms of the standard Windows thread scheduling. It is accurate up to about 20-50 milliseconds.

So that it's ok for user experience-dependent things. However it's absolutely inappropriate for real-time things.

Beside of this, there're much better ways to simulate keyboard/mouse events. Please see SendInput.

valdo
  • 12,632
  • 2
  • 37
  • 67
0

The sleep() function will return before the desired delay when the requested delay is shorter than the time left until the next interrupt occurs. But this only points out that you want to sleep for a shorter period of time than currently is supported by your system. It is advisable to setup the multimedia timer resource to a higher interrupt frequency to obtain better matching of the observed sleep delay with respect to the desired delay.

The the comments in the following threads:

How to get an accurate 1ms Timer Tick under WinXP

Sleep Less Than One Millisecond

Community
  • 1
  • 1
Arno
  • 4,994
  • 3
  • 39
  • 63
0

The command Sleep() will ensure that thread is suspended at least the amount of time which is given as argument. Operating system does not guarantee it. For detailed discussion you can refer the below post how is sleep implemented at OS level?

Community
  • 1
  • 1
mohan.t
  • 257
  • 5
  • 12