c# - Timer resets after 60 seconds -
below code i'm attempting use elapsed timer on desktop task timer we're building. right when runs counts 60 seconds , resets , doesn't ever add minutes.
//tick timer checks see how long agent has been sitting in misc timer status, reminds them after 5 mintues ensure correct status used private void statustime_tick(object sender, eventargs e) { counter++; //the timespan handle push elapsed time in seconds label can update user //this shouldn't require background worker since it's small app , nothing resource heavy var timespan = timespan.fromseconds(actualtimer.elapsed.seconds); //convert time in seconds format requested user displaycounter.text=("elapsed time in " + statusname+" "+ timespan.tostring(@"mm\:ss")); //pull thread updating ui application.doevents(); }
quick fix
i believe problem using seconds
0-59. want use totalseconds
existing code:
var timespan = timespan.fromseconds(actualtimer.elapsed.totalseconds);
comments
however, doesn't make lot of sense use timespan
object directly:
var timespan = actualtimer.elapsed;
also, can't see application, expect not need call application.doevents();
. ui should update automatically when has chance... if doesn't want @ moving whatever code blocking ui different thread.
recommendation
with said, recommend don't use timer track elapsed time @ all. timers can lose accuracy on time. best approach store current system time when start process, when need display 'timer' on-demand calculation @ point.
a simple example explain mean:
datetime start; void starttimer() { start = datetime.now; } void updatedisplay() { var timespan = datetime.now.subtract(start); displaycounter.text = "elapsed time in " + statusname + " " + timespan.tostring(@"mm\:ss")); }
you use timer call updatedisplay
method @ regular intervals:
void statustime_tick(object sender, eventargs e) { updatedisplay(); }
Comments
Post a Comment