So I have a method that has to run every 30 seconds for upto 2 hours.
My code is:
private void btnJSON_Click(object sender, RoutedEventArgs e)
{
Task.Factory.StartNew(() =>
{
//Timing Logic
var geTimerDelay = 2;
Stopwatch s = new Stopwatch();
s.Start();
while (s.Elapsed < TimeSpan.FromHours(geTimerDelay))
{
Stopwatch s30 = new Stopwatch();
s30.Start();
while (s.Elapsed < TimeSpan.FromSeconds(30))
{
//Method To Run
}
s30.Stop();
}
s.Stop();
});
}
Am I doing it correctly (that is achieving the time-gap as mentioned) or is there a correct and/or more time - precise way to do it?
I need to know because I am access data from specific urls and sometimes I am getting null values, maybe due to too frequent access.
Thanks.
EDIT: This gave me an idea of not using a timer, for no specific reason.