Code to Calculate Time Left on Timer

I'm not sure how useful this will be. It's towards the basic end of the programming scale but it solves the problem of showing a user how much time is left in a timer. This is useful if, for example, a user is able to set an x amount of minutes until a certain action is executed. Wouldn't it be nice if the user could check and see how much longer untill the action in question will take? This wouldn't be too useful if the timer was only set for a few milliseconds but it would be if dealing with minutes so thats what this was written for. It would be easy to re-write it for milliseconds, seconds, or even hours just by changing "60000" to the appropriate value.

But anyway, on with the code...

To use the following code, you must first add an OnTimer function to the class you will use this code in. Also to that class, you must add several member variables, They are declared as follows:

char m_strTimeLeft[10];

int m_nTimeLeft;

m_nTimer is an int linked to an edit control where the user will input the amount of minutes the timer will be set for.

m_ctlTimeLeft is a CEdit variable linked to the edit control where the program will output how many minutes are left until the timer expires.

The code is pretty self-explanatory.. Just add the code to your source file, change or add anything you need to, and you're set...

//Add this code to function where the timer is started:<

//Get Info from dlg box
UpdateData();                                     

//Set a seperate variable to the amount of minutes
m_nTimeLeft = m_nTimer;                           

//Convert the amount of minutes to a string
_itoa(m_nTimeLeft, m_strTimeLeft, 10);            

//Post that character to the "Time Left" control
m_ctlTimeLeft.SetWindowText(m_strTimeLeft);       

//If the timer wasn't set for 0 minutes, continue
if(m_nTimer !=0)                                  
{
 //Set a timer for one minute.. 
 //You may want to change this to use millseconds, seconds, or hours
 SetTimer(1, 60000, NULL);                     
}

//Add this code to your OnTimer function:
//If it was our timer that called this function do the following:
if(nIDEvent == 1)                                 
{
 //Decrease the amount of minutes left by one
 m_nTimeLeft--;                                
 
 //Convert the amount of minutes left to a string
 _itoa(m_nTimeLeft, m_strTimeLeft, 10);        

 //Update the "Time Left" control
 m_ctlTimeLeft.SetWindowText(m_strTimeLeft);	  

 //If there are no minutes left kill the timer and do whatever else needed:
 if(m_nTimeLeft == 0)						  
 {
  KillTimer(1);							  
  //The amount of minutes the user specified has expired, 
  //now execute the action to take place:
 }
}


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds