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!

building a delay into a program

Status
Not open for further replies.

Vervoortje

Technical User
May 5, 2003
6
BE
i have a MFC project dialog based.
and i would like to wait a few seconds between two actions, is that possible ? Msdn mibrary didn't gave me any results.

i know it is in turbo pascal but "delay(1000);" doesnt really seem to work ;)

Thx in advance
 
Code:
VOID Sleep(
  DWORD dwMilliseconds   // sleep time
);

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
Well, I don't know about any delay functions since I am fairly new to c++ but I do know that you could make a for loop that doesn't do anything but increment a counter and loop several times. That could vary though on different systems so maybe use <time.h>....here is some psuedocode:

...
DECLARE VAR1
DECLARE VAR2
VAR1 = CURRENT TIME IN SECONDS
WHILE(VAR2-VAR1 < HOW MANY SECONDS TO WAIT)
{
VAR2 = CURRENT TIME IN SECONDS
}
...


Yeah, if that makes sense. It would loop however many seconds based on the system clock. I am not sure on exact code but that is just an idea.
 
Jeremy,

Your idea did work very well in the stone-age of dos, when the computer had nothing else to do while waiting. But running a multi-tasking os like Windows, Unix or Linux, this approach is not the way to go, because other programs can not get the processor while it is waiting for your program. Like Palbano wrote, Sleep is the right solution, this os-function lets other processes use the cpu while waiting.


Marcel
 
Be advised that Sleep and other wait functions (i.e., WaitForSingleObject et al.) will block your program from processing any messages¹. If you want to wait for something and also process messages through your message loop, use the SetTimer function. This will either execute a user-defined callback function when the interval expires, or it will post a WM_TIMER to the message queue.

I REALLY hope that helps.
Will

______________________________________________
¹ Not including MsgWaitForMultipleObjects(Ex)!!!
 
Sleep(ms) means that the thread, from which it is called, will give up its time slice to the operating system for (ms) time. So not only message are not processed, but nothing is processed during (ms).
 
You can also create a messageloop, which will be looped through a certain amount of time. Use settiemer and waitforsingle object etc. to know when the waiting timer has been elapsed in the loop. This way at least your messages will still be processed, while doing &quot;nothing&quot;. Use &quot;peekmessage&quot; instead of &quot;getmessage&quot; and still include a &quot;sleep&quot; of some millisceonds, to prevent your processor from going up to 100%. Or use sleep(0), which will allow other threads to execute normally (but still your thread will get the proc up to 100%).


Greetings,
Rick
 
I've coded a stopwatch with a very hi resolution.
It uses WIN32 functions 'QueryPerformanceFrequency' and 'QueryPerformanceCounter'. I made a little console test program with an animated prompt, to show how to apply the stopwatch object.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <conio.h>

class StopWatch
{
private:
LARGE_INTEGER liFreq;
LARGE_INTEGER liStart;
LARGE_INTEGER liStop;

public:
BOOL bAvailable;

public:
StopWatch()
{
liStart.QuadPart = liStop.QuadPart = 0;
bAvailable = QueryPerformanceFrequency(&liFreq);
}

void Start()
{
QueryPerformanceCounter(&liStart);
}

double Stop()
{
QueryPerformanceCounter(&liStop);
return (((double)liStop.QuadPart - (double)liStart.QuadPart) / (double)liFreq.QuadPart);
}

void Reset()
{
liStart.QuadPart = liStop.QuadPart = 0;
}
};


void main()
{
StopWatch sw;
double dElapsed, dInterval = 0.0625; // 1/16 sec.
static char* sza[] = {
&quot;PROMPT->%c\r&quot;,
&quot;PROMPT\\>%c\r&quot;,
&quot;PROMPT|>%c\r&quot;,
&quot;PROMPT/>%c\r&quot;
};
long i=0;
char c = ' ';
puts(&quot;Press Q to quit...\n&quot;);


sw.Start();

while(1){

if(kbhit()){
c = toupper(getch());
if(c == 'Q')return;
}

dElapsed = sw.Stop();
if(dElapsed < dInterval){

continue;

}else{

printf(sza[i++], c);
if(i == 4) i = 0;

sw.Start();
}
}
}

If you need very preciese interval measurement within a loop, this class can do the trick. Using StopWatch in a separate thread you can build your own calling back timer.

MSDN Library says (at 'QueryPerformanceFrequency' pages)that not all systems have a performance counter available. But I guess most all pentium class pc's do.
 
Did someone test it on his/her system, found any bugs or found the stopwatch usefull? Any feedback is welcome.
 
a short but noisful way is to use &quot;Beep&quot;
Beep(440,1000) will sound for 1 second diapason &quot;a&quot;
It is a little disturbing, but it works.
Sometimes I test with sound instead of debug.


Greetings Andreas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top