Windows "Sleep" delay alternative

Whats the alternative to using windows' "Sleep" in the windows environment? Specifically, I'm trying create a delay when the player touches a switch (its a boolean switch/*on/off*/) so that the switch doesn't remain turned on at the time the collision is true.

Is a an alternative to sleep with this problem?

Or do I have to use a time counter as an alternative? and if so, how would I do it?
Is this for some kind of game? If so, you already have a main loop that does a fixed amount of logic ticks per second.
Just count ticks until you reach the desired target and then turn off the switch.
how do I do that? codewise.
Have you heard of the continue keyword? It will force a loop it's in to skip to its next iteration. You could use that to effectively skip certain operations that you don't want to happen in a loop while (was it?) the button is on.

Although... I'll admit that I am not 100% of what you need. :/

-Albatross
Last edited on
1
2
3
4
startTicks = getTickCount();

if( getTickCount() > startTicks + 50 ) //10 ticks per second, 50 = 5 seconds
    turnOffButton();

Something like that?
I have this code ,I see it in a web page before but I don't sure if it work as you want

code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <windows.h>
#include <ctime>

using namespace std;

int main()
{
 
  int seconds =5;
  clock_t delay = seconds *CLOCKS_PER_SEC;
  clock_t start = clock();
  while(clock() - start < delay){
  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);}
  SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
    return 0;
}

running in visual studio 2008

Bye,
No, clock() is unsuitable for measuring time.
On Windows, use GetTickCount() or gettimeofday() for a more portable alternative.
Topic archived. No new replies allowed.