Variable latency in Windows networking <p>I have a PLC that sends UDP packets every 24ms. "Simultaneously" (i.e. within a matter of what should be a few tens or at most hundreds of microseconds), the same PLC triggers a camera to snap an image. There is a Windows 8.1 system that receives both the images and the UDP packets, and an application running on it that should be able to match each image with the UDP packet from the PLC.</p> <p>Most of the time, there is a reasonably fixed latency between the two events as far as the Windows application is concerned - 20ms +/- 5ms. But sometimes the latency rises, and never really falls. Eventually it goes beyond the range of the matching buffer I have, and the two systems reset themselves, which always starts back off with "normal" levels of latency.</p> <p>What puzzles me is the variability in this variable latency - that sometimes it will sit all day on 20ms +/- 5ms, but on other days it will regularly and rapidly increase, and our system resets itself disturbingly often.</p> <p>What could be going on here? What can be done to fix it? Is Windows the likely source of the latency, or the PLC system?</p> <p>I 99% suspect Windows, since the PLC is designed for real time response, and Windows isn't. Does this sound "normal" for Windows? If so, even if there are other processes contending for the network and/or other resources, why doesn't Windows ever seem to catch up - to rise in latency when contention occurs, but return to normal latency levels after the contention stops?</p> <p>FYI: the Windows application calls <code>SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS )</code> and each critical thread is started with <code>AfxBeginThread( SomeThread, pSomeParam, THREAD_PRIORITY_TIME_CRITICAL )</code>. There is as little as possible else running on the system, and the application only uses about 5% of the available Quad-core processor (with hyperthreading, so 8 effective processors). There is no use of <code>SetThreadAffinityMask()</code> although I am considering it.</p>