Thread: System.Timers.Timer problem

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    System.Timers.Timer problem

    I have a System.Timers.Timer setup like so:

    Code:
    System.Timers.Timer timer = new System.Timers.Timer();
     
    timer.AutoReset = false;
    timer.Interval = frametime;
    timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    I than start the timer only once calling the timer.Start() method and after the period of time specified it executes the event and than stops like it is suposed to. However in that event I set the interval of the timer to a different value like so:

    Code:
    if (currentframe == framecount - 1)
    {
        timer.Interval = framepausetime;
    }
    else
    {
        timer.Interval = frametime;
    }
    When I set the interval it starts the timer again without me calling Start. I checked to make sure that it wasn't enabled and it is not enabled when I set the interval. Why does it keep starting when I set the interval?

    It also apears that calling Stop() on the timer doesn't stop it.
    Last edited by Rune Hunter; 01-29-2008 at 12:04 PM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    I dont know the exact cause but I read an article on msdn:

    Quote Originally Posted by http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx#S2
    ...
    As I mentioned earlier, the System.Timers.Timer class members are very similar to those of the System.Windows.Forms.Timer class. The biggest difference is that System.Timers.Timer is a wrapper around Win32 waitable timer objects and raises an Elapsed event on a worker thread rather than a Tick event on the UI thread. The Elapsed event must be connected to an event handler that matches the ElapsedEventHandler delegate. The event handler receives an argument of type ElapsedEventArgs.
    Above and beyond the standard EventArgs members, the ElapsedEventArgs class exposes a public SignalTime property, which contains the exact time the timer elapsed. Because this class supports access from different threads, it is conceivable that the Stop method may be called on a thread other than the thread that is used for the Elapsed event. This could potentially result in the Elapsed event firing even after the Stop method has been called. You can deal with this by comparing the SignalTime property to the time the Stop method was called.
    The System.Timers.Timer class also provides an AutoReset property that determines if the Elapsed event should fire continuously or just once. Keep in mind that resetting the Interval property after the timer has started will reset the current count back to zero. For example, if the interval is set to 5 seconds and 3 seconds have already elapsed before the interval is changed to 10 seconds, the next timer event will be 13 seconds from the last timer event.
    ...
    Probably has something to do with this...

    I cant imagine a timer firing off by itself so ...

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ahh ha there we go. Okay yea working with that helped a lot, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM