Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i run a timer in the background and let the rest of the ....

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How can I let a simple 30 second countdown timer (I used a for loop with Sleep(1000); to make a timer)run, while other parts of my program are running during its course?

for(x=30;x>-1;x--) // Here's the timer
{
printf("%i", x);
Sleep(1000);
SetConsoleCursorPosition(hOutput, timer);
printf(" ");
SetConsoleCursorPosition(hOutput, timer);
}

//... and a few lines down

while(1)
{
INPUT_RECORD InputRecord;
DWORD Events=0;

ReadConsoleInput(hInput,&InputRecord,1,&Events);

if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_UP)
{
SetConsoleCursorPosition(hOutput, Info.location);
printf(" ");
Info.location.Y--;
SetConsoleCursorPosition(hOutput, Info.location);
printf("%c", 1);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)
{
SetConsoleCursorPosition(hOutput, Info.location);
printf(" ");
Info.location.Y++;
SetConsoleCursorPosition(hOutput, Info.location);
printf("%c", 1);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
SetConsoleCursorPosition(hOutput, Info.location);
printf(" ");
Info.location.X--;
SetConsoleCursorPosition(hOutput, Info.location);
printf("%c", 1);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
SetConsoleCursorPosition(hOutput, Info.location);
printf(" ");
Info.location.X++;
SetConsoleCursorPosition(hOutput, Info.location);
printf("%c", 1);
}

}
if(Info.location.X == 55 && Info.location.Y == 21)
{
SetConsoleCursorPosition(hOutput, Stats.victory);
printf("You've won! Congratulations!\n");
Sleep(2000);
SetConsoleCursorPosition(hOutput, Stats.victory);
printf(" ");
SetConsoleCursorPosition(hOutput, Stats.home );
break;
}

}

//and thats the loop that I want to run during the timer's running... its the beginnings of a game in which the player has 30 seconds to reach a specified goal on the screen. I'm sure there's a way to let both the timer and the game loop run at the same time... if someone could help it would be great. thanks.
 
If you are using a UNIX clone you cna use signal handling to create a timer. One way is to use the alarm() call.

The alarm() system call instructs the alarm clock of the calling process to send the signal SIGALRM to the calling process after the number of real time seconds specified by sec have elapsed
You can write your own signal handler for SIGLARM which will can invoke whatever function your code wants to execute at the end of the timer.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
#include <process.h>

start your timer with _beginthreadex (not CreateThread, because you're using C-runtime function printf)

end the timer with _endthreadex

compile with some multithreading-option, like /MT or /MD Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top