Does windows use the time in sleep() for housekeeping? lazarus / 34-Windows ---------------------------------------------------------------------- Sander · 2023-10-15 12:03 Hello all, I am wondering if I can use "sleep()" to coax windows into stealing time from my program when I want it to as opposed to when I don't want it to. This ties into how windows does it's time slicing of which I know nothing. Another way of putting the question is what happens during "sleep" ? I'd like to stay out of "critical sections" because I don't know at which point the duration of these could/may/will upset Windows. The section of the app where  I would like to minimize "stolen time"  can be up to a second long. My (console) app reads a stack of voltmeters, after which it waits around until it's time for the next sample. If at all possible, I would like  like to shift as much windows housekeeping into the idle time as I can. I currently set a target time  for the next sample and loop a  "now" read to see if I'm there yet. There are likely ways of doing this which gives some of that time to win ? Sleep came to mind... Sander ---------------------------------------------------------------------- cdbc · 2023-10-15 13:34 Hi Hmmm, how about PRtlEvent? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RTLeventWaitFor(fEvent,aTimeout);Regards Benny ---------------------------------------------------------------------- Dzandaa · 2023-10-15 14:09 Hi, I don't know if this procedure can be useful to you: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ********************************************// ***** Real Sleep with Messages Process *****// ********************************************procedure RealSleep(DelayTickCount: QWORD);var  StartTickCount : QWORD;begin  StartTickCount := GetTickCount64;  while (GetTickCount64 < StartTickCount + DelayTickCount) and (not Application.Terminated) do   begin    Application.ProcessMessages;    Sleep(10); // You can change that value to a shorter time   end;end; B-> ---------------------------------------------------------------------- Sander · 2023-10-15 17:34 Quote from: cdbc on October 15, 2023, 03:34:13 pmHi Hmmm, how about PRtlEvent? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i RTLeventWaitFor(fEvent,aTimeout);Regards Benny Thanks Benny, I'll try it and see if it makes any difference. Sander ---------------------------------------------------------------------- Sander · 2023-10-15 17:41 Quote from: Dzandaa on October 15, 2023, 04:09:28 pmHi, I don't know if this procedure can be useful to you: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ********************************************// ***** Real Sleep with Messages Process *****// ********************************************procedure RealSleep(DelayTickCount: QWORD);var  StartTickCount : QWORD;begin  StartTickCount := GetTickCount64;  while (GetTickCount64 < StartTickCount + DelayTickCount) and (not Application.Terminated) do   begin    Application.ProcessMessages;    Sleep(10); // You can change that value to a shorter time   end;end; B-> Thanks Dzandaa, would you happen to know what "sleep" actually does ? Does it suspend my thread and meanwhille does other housekeeping? If so, it may make a difference. Sander ---------------------------------------------------------------------- hansotten · 2023-10-15 17:58 If you want the OS a chance to do something,  use: Application.ProcessMessages; It is not only Windows, it works for other OS'es too like Linux etc. ---------------------------------------------------------------------- Sander · 2023-10-15 20:34 Dankjewel, Hans. Sander ---------------------------------------------------------------------- alpine · 2023-10-16 07:16 @Sander Perhaps the right way would be to start a separate thread to do the sampling using RTLeventWaitFor for the pace (as cdbc suggested) and transfer the samples to the GUI via a thread safe queue. That way you can use a simple TTimer at the GUI side to empty the samples queue. ---------------------------------------------------------------------- 440bx · 2023-10-16 08:59 What "Sleep" does: it causes the scheduler to stop executing the thread that called Sleep and prevents the thread from being scheduled again until the sleep time has elapsed. In other words, a thread that calls Sleep will _not_ get a chance to do anything at all until the sleep time has elapsed. One _very important_ "wrinkle" with "Sleep" is that its accuracy can vary quite a bit.  To minimize the variation - at least in Windows - there are APIs that can be called to increase its resolution.  In your case in particular where you are reading a stack of hardware devices, some precision is usually quite desirable and likely important too to ensure no readings are missed. As alpine suggested, you should have a separate/dedicated thread to read the devices.  The GUI thread should _not_ be reading the devices.  A nice way of implementing the information sharing between the GUI thread and the device reading thread would be to set aside a block of memory which is only read, never written by the GUI thread and write-only by the device reading thread.    The device reading thread would call sleep for a number of milliseconds, that would minimize the CPU usage and would "schedule" the reading to occur every "n" milliseconds (as specified in the call to Sleep.) If the block of memory is written by only one thread then it is possible to share access to it without using synchronization elements (such as a critical section) which would make everything run smoother. HTH.