Hi,
I don't understand why sometimes, a loop running in a thread takes
much more time than usual.
I have a thread that must call each 20 ms a C++ unmanaged function.
I made a C++ managed wrapper for calling this function from C# (see
below _serialChannel. ItTask ()).
It works great but for this timing problem.
Here below, the main loop for this thread :
while (true)
{
Util.TraceLine ("before WaitOne");
if (_itTaskEvent.W aitOne (25, false))
{
Util.TraceLine ("ItTaskProc end asked");
break;
}
Util.TraceLine ("after WaitOne");
Util.TraceLine ("before ItTaskProc");
_serialChannel. ItTask ();
Util.TraceLine ("after ItTaskProc");
progressBarItTa skThread.Perfor mStep ();
if (progressBarItT askThread.Value >= progressBarItTa skThread.Maximu m)
progressBarItTa skThread.Value = progressBarItTa skThread.Minimu m;
dtLast = dtCurrent;
dtCurrent = DateTime.Now;
Util.TraceLine ("dtCurrent= " + dtCurrent.ToStr ing ("HH:mm:ss:f ff: ")
+
", dtLast=" + dtLast.ToString ("HH:mm:ss:f ff: "));
TimeSpan diff = dtCurrent.Subtr act (dtLast);
if (_minItTaskElap sedTime > diff)
{
_minItTaskElaps edTime = diff;
Log ("New mininimum ItTaskElapsedTi me = " + _minItTaskElaps edTime);
}
if (_maxItTaskElap sedTime < diff)
{
_maxItTaskElaps edTime = diff;
Log ("New maximum ItTaskElapsedTi me = " + _maxItTaskElaps edTime);
}
}
Here is the log output :
09:14:06:546: ItTask thread: before WaitOne
09:14:06:578: ItTask thread: after WaitOne
09:14:06:578: ItTask thread: before ItTaskProc
09:14:06:578: ItTask thread: after ItTaskProc
09:14:06:578: ItTask thread: dtCurrent=09:14 :06:578: ,
dtLast=09:14:06 :546:
09:14:06:578: ItTask thread: before WaitOne
09:14:06:609: ItTask thread: after WaitOne
09:14:06:609: ItTask thread: before ItTaskProc
09:14:06:609: ItTask thread: after ItTaskProc
09:14:06:609: ItTask thread: dtCurrent=09:14 :06:609: ,
dtLast=09:14:06 :578:
09:14:06:609: ItTask thread: before WaitOne
09:14:06:640: ItTask thread: after WaitOne
09:14:06:640: ItTask thread: before ItTaskProc
09:14:06:656: ItTask thread: after ItTaskProc
09:14:06:656: ItTask thread: dtCurrent=09:14 :06:656: ,
dtLast=09:14:06 :609:
09:14:06:796: ItTask thread: before WaitOne
09:14:06:828: ItTask thread: after WaitOne
09:14:06:828: ItTask thread: before ItTaskProc
09:14:06:828: ItTask thread: after ItTaskProc
09:14:06:828: ItTask thread: dtCurrent=09:14 :06:828: ,
dtLast=09:14:06 :656:
09:14:06:828: ItTask thread: New maximum ItTaskElapsedTi me =
00:00:00.171875 0
As you can see, the loop runs usually in 30-40 ms but the last one run
in 171 ms !
I set priority for this thread to ThreadPriority. Highest.
Can anyone help me to understand this behaviour ?
Thanks in advance.
Regards,
Droopy.