Timers are declared outside of the ProgramStarted() method, and need at least one parameter: an interval, which is the time period between ticks. The interval can be specified in milliseconds by passing an integer as a parameter, or a TimeSpan object. A second optional parameter specifes the behaviour of the timer.
Is the timer action influenced by the GC if the GT.Timer is declared in the Program.Started() method.
Thanks, Roland
I have run the following in the emulator, and got an answer to the question. I would assume it would be the same on a real device.
While it works with Gadgeteer, which does all sorts of things under the covers, with standard NETMF timers I suspect it would not work.
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace GTimerTest
{
public partial class Program
{
private GT.Timer gcTimer = new GT.Timer(5000);
void ProgramStarted()
{
GT.Timer tickTimer = new GT.Timer(1000);
tickTimer.Tick += tickTimer_Tick;
gcTimer.Tick += gcTimer_Tick;
tickTimer.Start();
gcTimer.Start();
}
void gcTimer_Tick(GT.Timer timer)
{
Debug.GC(true);
Debug.Print("GC");
}
void tickTimer_Tick(GT.Timer timer)
{
Debug.Print("Tick");
}
}
}
@ Mike -
Thank you Mike, yes, it seems to work.
I just tried on a Cerbuino Bee
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace GadgeteerApp2
{
public partial class Program
{
byte[] myByteArray;
void ProgramStarted()
{
GT.Timer timer = new GT.Timer(500); // every 500ms
timer.Tick +=timer_Tick;
timer.Start();
Debug.Print("Program Started");
}
void timer_Tick(GT.Timer timer)
{
Debug.Print(timer.Interval.ToString());
myByteArray = new byte[20000];
Debug.GC(true);
}
}
}
For how long? Forever? Everything I know about C# and .NET says that the timer should eventually be garbage collected. However, your
won't because the timer is in use, specifically, waiting for
```cs]void timer_Tick(GT.Timer timer)[/code
to return.
I would always declare it class scope, because in .NET full-sized, I've seen timers GC'd once out of scope. .NETMF may be different, but I would consider it bad practice to do it otherwise, because it's not guaranteed to not change, and declaring it in a method that will always be run buys you nothing.