You must be logged in to reply.

Page 1 of 1 out of 6 messages.

ExtendedTimer stop

Posted 5mth ago
by leforban | Hero | 15,714 exp
Posted 5mth ago
by leforban | Hero | 15,714 exp
Hi everyone,

I declare an ExtendedTimer that I need to pause in some cases. I am using the Change method but it seems that the event is still fired (at least one time) after calling Timer.Change(Timeout.Infinite, Timeout.Infinite); Is it possible that the timer is added to the thread pool and changed only after being run?
1 like
Reply #1 — Posted 5mth ago (modified)
by willgeorge | King | 33,542 exp
Reply #1 — Posted 5mth ago (modified)
by willgeorge | King | 33,542 exp
@leforban -

I have not noticed your problem.

I use:
// Timer(TimerCallback callback, object state, int dueTime, int period);
// dueTime - The amount of time to delay before callback is invoked, in milliseconds.
// Specify Timeout.Infinite to prevent the timer from starting. Use zero(0) to start the timer immediately.

public static Timer t;
t = new Timer(MyTimerCallback, true, Timeout.Infinite, 0); << I use 0 not Timeout.Infinite

private static void MyTimerCallback(object state)
{
     Debug.Print("Timer Tick");
}
//

To start the timer I use something like this
t.Change(10,1000);
Reply #2 — Posted 5mth ago
by leforban | Hero | 15,714 exp
Reply #2 — Posted 5mth ago
by leforban | Hero | 15,714 exp
@willgeorge - I am using Extended Timer where the last parameter of Change method is the time interval between callback method that is why I specify Infinite to disable periodic signaling. I also specify Infinite for the due time in order to prevent the timer from staring but it seems that the previous timer is already in the thread pool and can't be stop at this moment... I 'll try to make a code that reproduce this phenomenon.
Reply #3 — Posted 5mth ago
by leforban | Hero | 15,714 exp
Reply #3 — Posted 5mth ago
by leforban | Hero | 15,714 exp
I wrote a code to reproduce this and it works properly so the change method is OK. The problem is not the timer itself but the way I was using them (several instances of timer was modifying the same variables...)
Reply #4 — Posted 5mth ago
by willgeorge | King | 33,542 exp
Reply #4 — Posted 5mth ago
by willgeorge | King | 33,542 exp
@leforban -

Thanks for the explanation on your usage.
Reply #5 — Posted 5mth ago
by leforban | Hero | 15,714 exp
Reply #5 — Posted 5mth ago
by leforban | Hero | 15,714 exp
@willgeorge - Here's the sample code that demonstrates Change method works well

using System;
using System.Threading;
using Microsoft.SPOT;

namespace TestTimer
{
    public class Program
    {
        public static void callback_timer(object state)
        {
            Debug.Print(DateTime.UtcNow.ToString());
        }

        public static ExtendedTimer ext_timer;

        public static void Main()
        {
            Debug.Print(Resources.GetString(Resources.StringResources.String1));
            ext_timer = new ExtendedTimer(callback_timer,null,500, Timeout.Infinite);;
            int i = 0;
            
            while (true)
            {Thread.Sleep(100);
                if (ext_timer != null)
                {
                    if(i<10)
                    {
                        Debug.Print("change 100");
                        ext_timer.Change(100,Timeout.Infinite);
                        i++;
                    }
                    else
                    {
                        Debug.Print("infinite");
                        ext_timer.Change(Timeout.Infinite, Timeout.Infinite);
                        i++;
                    }
                    if (i == 20)
                    {
                        i = 0;
                    }
                }
                
            }
       }

       
    }
}

Page 1 of 1 out of 6 messages.

You must be logged in to reply.