CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2008
    Posts
    17

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Need to add pause to existing C++ program

    Define "pause".
    Note that this
    Code:
    Sleep(10000);
    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

  3. #3
    Join Date
    Jul 2009
    Posts
    6

    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++ )
    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Need to add pause to existing C++ program

    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
    [/QUOTE]Well, why do you think that this loop will be executed for exactly 10 seconds? [

    Quote Originally Posted by lio_cs View Post
    ... or :

    #define DELAY 128000 // increase the delay value if you wish
    And how exactly has OP to *increase* this magic 128000 number to get exactly 10 seconds on any user PC?
    Victor Nijegorodov

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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 ?

  6. #6
    Join Date
    Jul 2009
    Posts
    6

    Re: Need to add pause to existing C++ program

    Quote Originally Posted by VictorN View Post
    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("&#37;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)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Need to add pause to existing C++ program

    Quote Originally Posted by lio_cs View Post
    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:
    Quote Originally Posted by straygrey View Post
    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);

    Quote Originally Posted by lio_cs View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured