-
July 17th, 2009, 04:35 AM
#1
Need to add pause to existing C++ program
I have been asked to add a pause to an existing C++ program.
The user does not want to use the Windows builtin scheduler for reasons they won't share with me.
Is the correct way of doing this something like:-
void Sleep(10000);
Last edited by straygrey; July 17th, 2009 at 04:42 AM.
Reason: spelling
-
July 17th, 2009, 05:02 AM
#2
Re: Need to add pause to existing C++ program
Define "pause".
Note that this will freeze your App. So its GUI will be completely "dead" for the 10 seconds and it would be at least surprised the users!
Is it what you want?
Victor Nijegorodov
-
July 17th, 2009, 05:17 AM
#3
Re: Need to add pause to existing C++ program
Or you can send your app to a loop for 10 sec
for(int i = 0; i < 1000000; i++) // A basic for loop
or :
#define DELAY 128000 // increase the delay value if you wish
void delay(void);
void delay(void)
{
long int t;
for (t=1; t< DELAY ; t++ )
}
-
July 17th, 2009, 05:24 AM
#4
Re: Need to add pause to existing C++ program
Victor Nijegorodov
-
July 17th, 2009, 06:29 AM
#5
Re: Need to add pause to existing C++ program
I have been asked to add a pause to an existing C++ program.
Is your program a commandline program or does it has a GUI ?
-
July 17th, 2009, 07:23 AM
#6
Re: Need to add pause to existing C++ program
Originally Posted by VictorN
QUOTE=lio_cs;1861155]Or you can send your app to a loop for 10 sec
for(int i = 0; i < 1000000; i++) // A basic for loop
Well, why do you think that this loop will be executed for exactly 10 seconds? [
And how exactly has OP to *increase* this magic 128000 number to get exactly 10 seconds on any user PC?
[/QUOTE]
Code:
#include <stdio.h>
#define DELAY 2560000000
struct my_time{
int hours;
int minutes;
int seconds;
};
void display(struct my_time *t);
void update(struct my_time *t);
void delay(void);
int main(void)
{
struct my_time systime;
systime.hours = 0;
systime.minutes = 0;
systime.seconds = 0;
for(;;)
{
update(&systime);
display(&systime);
}
return 0;
}
void update(struct my_time *t)
{
t->seconds++;
if(t->seconds==60)
{
t->seconds = 0;
t->minutes++;
}
if(t->minutes==60)
{
t->minutes = 0;
t->hours++;
}
if(t->hours==24) t->hours = 0;
delay();
}
void display(struct my_time *t)
{
printf("%02d:", t->hours);
printf("%02d:", t->minutes);
printf("%02d:\n", t->seconds);
}
void delay(void)
{
unsigned long int t;
for(t=1;t<DELAY;t++);
}
ok this will cause a 10 sec delay predefined by the programmer (DELAY)
-
July 17th, 2009, 07:27 AM
#7
Re: Need to add pause to existing C++ program
As Victor said, he needs to define pause and give more information about his app. Whatever it means, I believe it's relatively safe to say the solution doesn't lie in a busy/wait loop.
-
July 17th, 2009, 07:36 AM
#8
Re: Need to add pause to existing C++ program
Originally Posted by lio_cs
Well, why do you think that this loop will be executed for exactly 10 seconds? [
But it is what OP seemed to want to implement using Sleep(10000) which will freeze the current thread for exactly 10 seconds:
Originally Posted by straygrey
I have been asked to add a pause to an existing C++ program....
Is the correct way of doing this something like:-
void Sleep(10000);
Originally Posted by lio_cs
Code:
#include <stdio.h>
#define DELAY 2560000000
struct my_time{
int hours;
int minutes;
int seconds;
};
void display(struct my_time *t);
void update(struct my_time *t);
void delay(void);
int main(void)
{
....
return 0;
}
void update(struct my_time *t)
{
....
}
void display(struct my_time *t)
{
.....
}
void delay(void)
{
.....
}
ok this will cause a 10 sec delay predefined by the programmer (DELAY)
Well, I don't have enogh time to test your code; I just hope it will work...
The only question is: why waste time to develop/write/test some code that is supposed to do exactly the same as Sleep does?!
Besides. this code would just unusefully load CPU!
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|