I have a Timer
which I run every 10 seconds to sync local data in my app back to our servers.
public Timer syncTimer;
syncTimer = new Timer((o) =>
{
this.syncToServer();
}, null, 0, 10000);
I have a problem where if my syncToServer takes longer than 10 seconds to execute, it trips over itself. I am considering doing the following to pause the timer whilst it executes and then restart it once complete:
public Timer syncTimer;
syncTimer = new Timer((o) =>
{
syncTimer.Change(Timeout.Infinite , Timeout.Infinite); //Stop the timer
this.syncToServer();
syncTimer.Change(10000 , 10000); //Restart the timer
}, null, 0, 10000);
Is this the best way to achieve what I am after or is there a better way to stop it executing at the same time?