c# - My elapsed time is only going up to 60 -
how can create elapsed time method goes beyond 60 seconds , counts in seconds. current implementation repeats every 60 seconds.
code:
void timer_tick(object sender, eventargs e) { time = datetime.now.second.tostring(); //datetime.now.tolongtimestring(); } public void timesetup() { timer = new dispatchertimer(); timer.interval = new timespan(0, 0, 1); //timer.interval = timespan.fromseconds(1); timer.tick += timer_tick; timer.start(); }
no need make things harder necessary:
class timerclass { public int time; void timer_tick(object sender, eventargs e) { time++; } public void timesetup() { timer = new dispatchertimer(); timer.interval = new timespan(0, 0, 1); timer.tick += timer_tick; timer.start(); } }
this calls tick handler every second , counts number of times called. can imprecise measuring long periods of time. long run, use
time = (datetime.now - starttime).totalseconds;
where starttime initialized time when start timer.