Timers are sleeping

Hi,

I am using a G120 microcontroller. I have two problems with my timers. The first problem is Gadgeeter timer is not working. The other problem is (and it is a critical issue for me) netmf timer is sleeping after a while. What is my mistake?

Gadgeeter Timer code:

GT.Timer t2 = new GT.Timer(500, GT.Timer.BehaviorType.RunContinuously);
t2.Tick += new GT.Timer.TickEventHandler(main_Tick);
t2.Start();

static void main_Tick(GT.Timer timer)
{
Debug.Print(“Tick2”);
}

NETMF Timer code:

class TimerCls
{
public DateTime generalCheckTask;
public DateTime gsmSendTask;
public DateTime satSendTask;
public DateTime gsmSendTaskNew;
public DateTime satSendTaskNew;
public DateTime checkInputTask;
}

TimerCls cls = new TimerCls();
cls.generalCheckTask = new DateTime();
cls.gsmSendTask = new DateTime();
cls.satSendTask = new DateTime();
cls.gsmSendTaskNew = new DateTime();
cls.satSendTaskNew = new DateTime();
cls.checkInputTask = new DateTime();

Timer mainTimer = new Timer(new TimerCallback(mainTimerTick), cls, 30 * 1000, 500);

static void mainTimerTick(object o)
{

}

without seeing more code to understand how you start things and how you know they’ve stopped, I suspect you have been GCed. How long before this apparent stop occurs (that’s a pretty important factor you should be looking at). What does debug output show you at that time (another important debugging step).

Hi Brett,

Thank you for your quick response.

This is my start code. If you can send your email, I can share the project with you.

public static void Main()
        {
            Logger.LogInfo("Cihaz Çalışmaya Başladı...");
            int timeout = 1000 * 60;
            GHI.Processor.Watchdog.Enable(timeout);
            WDTCounterReset = new Thread(WDTCounterResetLoop);
            WDTCounterReset.Start();

            if (GHI.Processor.Watchdog.LastResetCause == GHI.Processor.Watchdog.ResetCause.Watchdog)
            {
                Logger.LogDebug("Watchdog Event: Watchdog did Reset");
            }
            else
            {
                Logger.LogDebug("Watchdog Event: Reset switch or system power");
            }

            startProgram();

            TimerCls cls = new TimerCls();
            cls.generalCheckTask = new DateTime();
            cls.gsmSendTask = new DateTime();
            cls.satSendTask = new DateTime();
            cls.gsmSendTaskNew = new DateTime();
            cls.satSendTaskNew = new DateTime();
            cls.checkInputTask = new DateTime();
            Timer mainTimer = new Timer(new TimerCallback(mainTimerTick), cls, 30 * 1000, 500);

            GT.Timer t2 = new GT.Timer(500, GT.Timer.BehaviorType.RunContinuously);
            t2.Tick += new GT.Timer.TickEventHandler(main_Tick);
            t2.Start();

            Thread.Sleep(Timeout.Infinite);
        }

sorry, it’s worth reducing your code to a simple example that proves your issue and post it here. And you still need to answer what you use to think it stops, and show debugging output.

Hi Brett,

I simplified the code and uploaded it to the wetransfer.

Download link

We are not aware of any problem with timers so why don’t you share a very small example that show the problem please

Actually, it is a random problem. Sometimes it is stopping 30 mins later sometimes stopping 3 hours later. I’ll share a log file with you.

Have you tested Gadgeeter timer? It is not working on the code.

Yeah we did, over 5 years ago. I even forgot how to use it :slight_smile:

Hi Gus,

I uploaded the log file to wetransfer, Gadgeeter timer is not working and NETMF timer has been stopped for a while.

Download Link

Gadgeteer is long gone and it is not supported as of years ago but we will try to help as much as possible. To do so, the we (and the community) need small example that they can see and try to determine what might be wrong., Most of us here no longer have VS2013/gadgeteer installed on our machines and we have no way of trying it.

… or just upgrade to SITCore and enjoy the latest and greatest with full support :slight_smile:

If you need I can share the gadgeeter installation file. Have you checked my log file? The timer has been stopped immediately after GC.

By the way, may I use SITCore with G120?

SITCore replaces G120.

I have written Debug.GC(true) in my timer tick method and GC is collecting (killing) my timer. What can I do for this issue?

keep your timer reference alive.

This is a good read https://docs.ghielectronics.com/software/tinyclr/tutorials/memory.html

Ok, I kept it, I am sharing the code below but it is still not working after a while. I don’t have a memory problem right now.

static Timer mainTimer;

        public static void Main()
        {
            Logger.LogInfo("Cihaz Çalışmaya Başladı...");
            int timeout = 1000 * 60;
            GHI.Processor.Watchdog.Enable(timeout);
            WDTCounterReset = new Thread(WDTCounterResetLoop);
            WDTCounterReset.Start();

            if (GHI.Processor.Watchdog.LastResetCause == GHI.Processor.Watchdog.ResetCause.Watchdog)
            {
                Logger.LogDebug("Watchdog Event: Watchdog did Reset");
            }
            else
            {
                Logger.LogDebug("Watchdog Event: Reset switch or system power");
            }

            startProgram();

            TimerCls cls = new TimerCls();
            cls.generalCheckTask = new DateTime();
            cls.gsmSendTask = new DateTime();
            cls.satSendTask = new DateTime();
            cls.gsmSendTaskNew = new DateTime();
            cls.satSendTaskNew = new DateTime();
            cls.checkInputTask = new DateTime();
            mainTimer = new Timer(new TimerCallback(mainTimerTick), cls, 30 * 1000, 500);

            Thread.Sleep(Timeout.Infinite);
        }

Are you properly using the watchdog timer? Is your board reseting?

Yes, I have tested it many times. Watchdog is working properly.

In the incomplete code you have posted, I do not see the watchdog timer being reset?

You can see this part on the code and watchdog is working right now.

static Thread WDTCounterReset;
        static void WDTCounterResetLoop()
        {
            while (true)
            {
                // reset time counter every 3 seconds
                Thread.Sleep(3000);

                GHI.Processor.Watchdog.ResetCounter();
            }
        }