Thread: Scheduled program

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    16

    Scheduled program

    I've been using something somewhat like this to keep track of time for a program, and it works fine... except that it seems to eat up all the computer's resources. Does anyone know how to have a program stay on a schedule while running in the background without sucking up all the computer's power?

    Code:
      
      while(!compareTime(timeH, timeM));
      cout << "End of job.";
    
    bool compareTime(int timeH, int timeM)
    {
        int timeH_now;
        int timeM_now;
        ofstream currentOut("current.txt");
        time_of_day now;
      
        now.get_time_string();
        currentOut << now.hour << " " << now.minute;
        currentOut.close();
      
        ifstream currentIn("current.txt");
        currentIn >> timeH_now >> timeM_now;
        currentIn.close();
      
        if(timeH_now == timeH && timeM_now == timeM)
           return true;
        else
           return false;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    If you're running in Windows, you can use the Sleep() command in the windows.h header.
    Code:
      while(!compareTime(timeH, timeM));
      cout << "End of job.";
    
    bool compareTime(int timeH, int timeM)
    {
        int timeH_now;
        int timeM_now;
        ofstream currentOut("current.txt");
        time_of_day now;
      
        now.get_time_string();
        currentOut << now.hour << " " << now.minute;
        currentOut.close();
      
        ifstream currentIn("current.txt");
        currentIn >> timeH_now >> timeM_now;
        currentIn.close();
      
        Sleep(1000); //sleeps for one second
    
        if(timeH_now == timeH && timeM_now == timeM)
           return true;
        else
           return false;
    }
    There are probably other ways to do it, but that's the one I know.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    16
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM