locked
Is there a problem with CHESS and System.Threading.Timer? RRS feed

  • Question

  • When running a test which uses a System.Threading.Timer under mchess or under the Chess MSTest host, I find that the timer calls back almost immediately regardless of the duration which has been set. I don't think it's a problem with our code and it's unlikely to be a problem with the timer itself - is Chess doing something strange?

    A minimal case to exhibit the problem is shown below.

    I'm running VS2008, .net 3.5 sp1 & ran this test by modifying your Bank Sample. I'm using V2.0.0.0 of System.dll and 3.5.0.0 of System.Core on 32 bit XP sp2 with an Intel Core 2 processor.

    [TestClass]
      public class TestTimer_c
      {
        const Int32 Duration_sc = 10000;
        Object Lock_m;
        volatile bool TimerExpired_m;
    
        /// <summary>
        /// Test method
        /// </summary>
        [TestMethod]
        [HostType("Chess")]
        [TestProperty("ChessDebug", "true")]
        public void TestTimer()
        {
          Lock_m = new Object();
          TimerExpired_m = false;
    
    
          DateTime Start = DateTime.Now;
          Timer Timer = new Timer(new TimerCallback(Callback), Guid.NewGuid(), Duration_sc, Timeout.Infinite);
    
          bool IsExpired = false;
    
          while(!IsExpired)
          {
            lock(Lock_m)
            {
              IsExpired = TimerExpired_m;
            }
    
            System.Threading.Thread.Sleep(10);
          }
    
          DateTime End = DateTime.Now;
          TimeSpan Diff = End-Start;
    
          Assert.IsTrue(Diff.TotalMilliseconds >= Duration_sc / 2, Diff.TotalMilliseconds.ToString());
        }
    
        /// <summary>
        /// Timer callback
        /// </summary>
        private void
        Callback
          (Object O)
        {
          lock (Lock_m)
          {
            TimerExpired_m = true;
          }
        } 


     

    • Edited by Juggler100 Wednesday, June 23, 2010 9:22 AM Reformatted code after editor lost formatting
    Wednesday, June 23, 2010 9:19 AM

Answers

  • As the saying goes: "It's a feature, not a bug". CHESS abstracts away from real-time. It tries the corner cases first (like the timer expiring). It is confusing, but by design. In the future, we may reconsider the design decision. It causes a lot of confusion.

    -- Tom

    Wednesday, July 14, 2010 4:47 AM