开发者

System.Timers.Timer wandering in Azure Worker Role

I have an Azure Worker Role with n (currently n = 2) instances. Each of these instances has a System.Timers.Timer on its main thread that executes every 20 seconds, spawning some work processes. When the role instances boot up initially, they synchronize themselves via internal endpoints so that the timers start at the same time (this works).

Over the course of even a day, these two timers with the same interval tend to wander - one will end up start开发者_StackOverflow社区ing several seconds before it used to, and the other may go the same direction or, worse, the other.

My run method is basically the following:

public override void Run()
{
    while (true) 
    {               
        GC.KeepAlive(this.LogTimer); // make sure garbage collection never touches the timer                                            
    }
}

Questions:

  1. What might be causing this?
  2. Would adding a Thread.Sleep(sleeptimeinMS) call to the while(true){} loop improve this by preventing the main thread from being busy all the time (to allow the timer thread to be somehow more free to do its duty?)?
  3. Is the right answer that I should try to resync the roles to some external clock periodically?
  4. Any radically different ways to implement this?

Thanks in advance for your time,

Alex


A System.Timers.Timer is not 100% accurate, it is limited by the thread scheduling in the OS. It makes good sense that two separate servers each running a Timer, eventually will get out of synch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜