Forum C# Time left on Timer

Time left on Timer

Postby azuanagames » September 11th, 2010, 6:21 pm

Hi!

Is there a way to determine how much time is left on a Timer?

For instance:
Code: Select all
currentTimer = ScheduleCallback(start, 5000);


When a player connects, I want to let them know how long they need to wait, e.g. there's 3 seconds left on timer.

I thought to use Environment.TickCount but that's not allowed. Should I use DateTime? I don't HAVE to have that much accuracy I guess...

Thanks
azuanagames
 
Posts: 157
Joined: April 29th, 2010, 10:59 pm

Re: Time left on Timer

Postby Oliver » September 13th, 2010, 10:14 am

Nope, there is no built in way to do that. It's easy however,

Just set a variable to DateTime.Now when you start the time, and use timeleft=DateTime.Now-startedTimer to get the time left.

Example:

Code: Select all
// starting a timer
var startTime = DateTime.Now;

//.... loads of time passes

var timeLeft = (DateTime.Now-startTime).TotalSeconds; // time left in seconds
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Time left on Timer

Postby azuanagames » September 13th, 2010, 1:27 pm

Sounds good. Thanks Oliver.
azuanagames
 
Posts: 157
Joined: April 29th, 2010, 10:59 pm


Return to C#