I am using OpenNETCF Timer2 (and previously System.Threading.Timer) in this example. The timer behaves very odd. After around 7-8 hours. It does a few random calls of Timer event function with a interval of 2 seconds instead of 1 second. And behaves normally for next 7-8 hours. Kindly guide, I have tried System.Threading.Timers but that appears to call timer event by 2 seconds instead of 1 second for each 2 minutes. How to fix this problem?
public partial class Form1 : Form
{
private OpenNETCF.Timers.Timer2 mTimer2;
private DateTime mLastSampleTime;
private Int32 Interval;
private Double ActualInterval;
private long ActualTickInterval;`enter code here`
StreamWriter file;
Boolean firstRun = true;
//private Timer sysTimer;
public Form1()
{
InitializeComponent();
/// <summary>
/// Timer object for Windows CE environment.
/// </summary>
///
// This text is always added, making the file longer over time
// if it is not deleted.
file = System.IO.File.AppendText("TimerLog.txt");
mTimer2 = new OpenNETCF.Timers.Timer2(1000);//1000 for actual test
mTimer2.AutoReset = true;
mTimer2.Elapsed += Timer2Event;
mTimer2.Start();
file.WriteLine("TImer Started");
}
private void Timer2Event(object sender,
OpenNETCF.Timers.ElapsedEventArgs e)
{
try
{
DateTime now = DateTime.UtcNow;
if (!firstRun) //do not calculate difference if first run
{
//ActualInterval = ((now - mLastSampleTime).TotalMilliseconds);//Difference between previous timestamp and current (in milliseconds)
ActualTickInterval = ((now - mLastSampleTime).Ticks);//Difference between previous timestamp and current (in ticks)
Interval = Convert.ToInt32((now - mLastSampleTime).TotalSeconds);//Interval as used in LAMP code
if (Interval >= 2) file.Write("-------->");//if Interval is 2 seconds, Mark it.
file.Write("UTC Now: " + now.ToString() +" Current Tick: "+now.Ticks.ToString() +" Actual Interval: " + ActualInterval + " Actual Tick Difference: "+ActualTickInterval.ToString()+" Deviation (Ideal=0) "+(ActualTickInterval-10000000).ToString()+"\r\n");
file.Flush();
}
else
{
firstRun = false;
}
mLastSampleTime = now;
}
catch (Exception exp)
{
file.Write("Logging Failed " + DateTime.UtcNow.ToString() + " exception: " + exp.Message+"\r\n");
}
}
}
}
The tick difference jumps directly from 10,000,000 to 2,00,000 for these specific cases. And the subsequent tick difference is 0. I.E 2 rows in the log file have the same timestamp.