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.WaitOne (25, false))
{
Util.TraceLine ("ItTaskProc end asked");
break;
}
Util.TraceLine ("after WaitOne");
Util.TraceLine ("before ItTaskProc");
_serialChannel.ItTask ();
Util.TraceLine ("after ItTaskProc");
progressBarItTaskThread.PerformStep ();
if (progressBarItTaskThread.Value >= progressBarItTaskThread.Maximum)
progressBarItTaskThread.Value = progressBarItTaskThread.Minimum;
dtLast = dtCurrent;
dtCurrent = DateTime.Now;
Util.TraceLine ("dtCurrent=" + dtCurrent.ToString ("HH:mm:ss:fff: ")
+
", dtLast=" + dtLast.ToString ("HH:mm:ss:fff: "));
TimeSpan diff = dtCurrent.Subtract (dtLast);
if (_minItTaskElapsedTime > diff)
{
_minItTaskElapsedTime = diff;
Log ("New mininimum ItTaskElapsedTime = " + _minItTaskElapsedTime);
}
if (_maxItTaskElapsedTime < diff)
{
_maxItTaskElapsedTime = diff;
Log ("New maximum ItTaskElapsedTime = " + _maxItTaskElapsedTime);
}
}
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 ItTaskElapsedTime =
00:00:00.1718750
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.