Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using timer to determine elapsed time 1

Status
Not open for further replies.

4getit

Programmer
Sep 15, 2006
8
US
I want to start a timer right when my program is launched and store the value of the time as a static variable so that I can call the timer function and get the time that the program has been running. My goal is to be able to determine the time from when the program was launched and pass it into a function I have created as the elapsed time. I've seen examples of timers but none of them show me how to call that timer function and take the elapsed time and pass it as an argument. Please help! Thanks in advance.
 
Why use a timer?

Just store a DateTime variable as soon as your program launches.

DateTime startTime = DateTime.Now;


Then to access the duration the app has been up for use

public TimeSpan GetDuration()
{
return DateTime.Now.Subtract(startTime);
}

or

public string GetDuration()
{
TimeSpan ts = DateTime.Now.Subtract(startTime);

return ts.ToString("hh:mm:ss");
}
 
That's a much easier way of doing it and it worked great. Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top