Reputation: 498
I want my timer to continue from the time I started it until now, I need help with the logic that uses timespan to calculate the elapsed time from the minutes the timer started to the current time, Here is what I have done:
CancellationTokenSource cts = _cancellationTokenSource; // safe copy
//Timer Starts here
_TimeSpan = TimeSpan.FromMinutes(TimeSheet.TotalTime);
//Increment timer by a second interval
Device.StartTimer(TimeSpan.FromSeconds(1), () => {
if (cts.IsCancellationRequested)
{
return false;
}
else
{
Device.BeginInvokeOnMainThread(() => {
_TimeSpan += TimeSpan.FromSeconds(1);
displaytime = _TimeSpan.ToString(@"h\:m\:s");
TimeSheet.TotalTimeForDisplay = displaytime;
Console.WriteLine("Timer");
Console.WriteLine(displaytime);
});
return true;
}
Upvotes: 0
Views: 416
Reputation: 89204
use System.Timers.Timer
// use whatever start time you need
DateTime start = DateTime.Now;
// 1000ms = 1s
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += OnTimedEvent;
timer.Start();
then
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
var elapsed = DateTime.Now - start;
}
Upvotes: 1