System.Timers.Timer Elapsed event executing after timer.Stop() is called
By : bala
Date : March 29 2020, 07:55 AM
it helps some times This is well known behavior. System.Timers.Timer internally uses ThreadPool for execution. Runtime will queue the Timer in threadpool. It would have already queued before you have called Stop method. It will fire at the elapsed time. To avoid this happening set Timer.AutoReset to false and start the timer back in the elapsed handler if you need one. Setting AutoReset false makes timer to fire only once, so in order to get timer fired on interval manually start timer again. code :
yourTimer.AutoReset = false;
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
// add your logic here
}
finally
{
yourTimer.Enabled = true;// or yourTimer.Start();
}
}
|
Xamarin.Forms 2.1.0.6521 - UWP Not Hitting OnStart / OnSleep methods
By : Satyam Gupta
Date : March 29 2020, 07:55 AM
|
Which is to use Stop/Start timer vs SynchronizingObject vs lock for System.Timers.Timer?
By : Trevor Ochocki
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The 4th solution (lock) is a dangerous one. Since the Elapsed event will be raised on a ThreadPool thread each time and you might potentially block many of them simultaneously, this could cause the ThreadPool to grow (with all the consequences). So that solution is a bad one.
|
Popping the navigation stack from App.OnSleep in iOS in Xamarin Forms
By : Anirudh
Date : March 29 2020, 07:55 AM
|
When I am navigating to a new Xamarin Page, content isnt displaying when I am running System.Timer.Timer code
By : user3128493
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have updated my code. The Device.BeginInvokeOnMainThread you tried before, could solve your problem. Maybe you try the wrong way. You could refer to the code below. code :
void t_Tick(object sender, EventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
try
{
TimeSpan ts = endTime - DateTime.Now;
string NewTimer = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
lblCountDown.Text = NewTimer;
if ((ts.TotalMilliseconds < 0) || (ts.TotalMilliseconds < 1000))
{
timer.Stop();
lblCountDown.Text = "The day has arrived";
}
}
catch (Exception ex)
{
string Error = ex.Message;
}
});
}
|