F
Faisal
Hi,
I'm encountering some strange problem with System.Threading.Timer. In
my application timer method is called before the interval is elapsed.
I specified 15 seconds as the interval but timer callback is called
around an interval of 14.96256.
I know that the timer is not reliable for accurate measurements. In
windows native programming I usually see the timer fires after
somtimes the interval elapsed(using the timer associated with a
window). But It's really strange that timer fires before the interval
in C#. Is this an expected behaviour.
I tested this in a sample application. Code is given below
System.Threading.Timer measImpedanceTimer;
private void button1_Click_1(object sender, EventArgs e)
{
measImpedanceTimer = new System.Threading.Timer(MeasureImpdedance,
null, 0, 15000);
}
bool bFirst = true;
DateTime testStart;
DateTime testPrev;
private void MeasureImpdedance(object step)
{
DateTime now = DateTime.Now;
if (bFirst)
{
testStart = now;
testPrev = testStart;
bFirst = false;
}
System.Diagnostics.Debug.WriteLine("Total: " + (now -
testStart).TotalSeconds +
" Diff Prev: " + (now - testPrev).TotalSeconds);
testPrev = now;
}
Output shows
Total: 0 Diff Prev: 0
Total: 14.96256 Diff Prev: 14.96256
Total: 29.92512 Diff Prev: 14.96256
Is there any way to solve this?
Also is this error-offset(15 - 14.96256) from the actual value is
consistant for all values of period?
Regards,
Faisal M
I'm encountering some strange problem with System.Threading.Timer. In
my application timer method is called before the interval is elapsed.
I specified 15 seconds as the interval but timer callback is called
around an interval of 14.96256.
I know that the timer is not reliable for accurate measurements. In
windows native programming I usually see the timer fires after
somtimes the interval elapsed(using the timer associated with a
window). But It's really strange that timer fires before the interval
in C#. Is this an expected behaviour.
I tested this in a sample application. Code is given below
System.Threading.Timer measImpedanceTimer;
private void button1_Click_1(object sender, EventArgs e)
{
measImpedanceTimer = new System.Threading.Timer(MeasureImpdedance,
null, 0, 15000);
}
bool bFirst = true;
DateTime testStart;
DateTime testPrev;
private void MeasureImpdedance(object step)
{
DateTime now = DateTime.Now;
if (bFirst)
{
testStart = now;
testPrev = testStart;
bFirst = false;
}
System.Diagnostics.Debug.WriteLine("Total: " + (now -
testStart).TotalSeconds +
" Diff Prev: " + (now - testPrev).TotalSeconds);
testPrev = now;
}
Output shows
Total: 0 Diff Prev: 0
Total: 14.96256 Diff Prev: 14.96256
Total: 29.92512 Diff Prev: 14.96256
Is there any way to solve this?
Also is this error-offset(15 - 14.96256) from the actual value is
consistant for all values of period?
Regards,
Faisal M