Saturday, September 25, 2010

Sleep until

long current = DateTime.Now.Ticks;
long start = DateTime.Parse(ConfigurationManager.AppSettings["StartDateTime"]).Ticks;
long timeout = TimeSpan.Parse(ConfigurationManager.AppSettings["Timeout"]).Ticks;
long sleep;

if (current < start) // until next timeout
sleep = timeout - ((current % timeout)) + (start % timeout);
else
sleep = start - current;

Thread.Sleep(new TimeSpan(sleep));

No comments:

Post a Comment