sfrazor 3 Posted March 6 (edited) Using OverbyteIcsSnippets as an example. Websocket CLIent section, after wsconnect is successful it falls in to this loop. while NOT AbortFlag do begin Application.ProcessMessages; if Application.Terminated then Break; if IcsTestTrgTick64(TrgTimerEnd) then Break; end; Mine is a console app and I do something almost identicle to this on startup. I connect then fall in to the main thread loop waiting for a signal to end. My application runs at 48% cpu usage just like the example. I realize this is a tight loop. Running as-is the OnWSFrameRecv will receive a 50M file in a few seconds. However when I try to address the high cpu issue and add a simple sleep(5), which drops the CPU down to less than 5 percent. The 50M file receive via the OnWSFrameRecv event now slows down to 9 minutes. Its like the receive is blocking. I'm not sure why a simple sleep would do this. Is the sleep cascading in the tight loop and causing the overall main thread to sleep much longer than the 5ms maybe? I know there is missing info here. The application is quite large but the websocket connect and receive is very basic. I rebuilt excluding throttling in the library but it didn't make any difference. Is there a better way this not CPU intensive to loop and wait for the termination? The goal is to receive large files via WS and not have the appl;ication idle at this high usage. Any help would be appreciated. Regards Edited March 6 by sfrazor Share this post Link to post
Angus Robertson 625 Posted March 6 Try Sleep(0) or MsgWaitForMultipleObjects before calling the pump, that is what the ICS synchronous methods do. BTW, Snippets is simple examples. Angus Share this post Link to post
sfrazor 3 Posted March 6 On 3/6/2025 at 7:27 PM, Angus Robertson said: Try Sleep(0) or MsgWaitForMultipleObjects before calling the pump, that is what the ICS synchronous methods do. BTW, Snippets is simple examples. I was sifting through the source looking for examples of this very issue. I found MsgWaitForMultipleObjects right after you posted. I had tried sleep(0) with no success. But the MsgWaitForMultipleObjects worked and now the CPU is back down to .3 % and file transfers are back to their normal fast speed. Thanks! Share this post Link to post
Anders Melander 1951 Posted March 6 On 3/6/2025 at 7:27 PM, Angus Robertson said: Try Sleep(0) https://joeduffyblog.com/2006/08/22/priorityinduced-starvation-why-sleep1-is-better-than-sleep0-and-the-windows-balance-set-manager/ 3 Share this post Link to post
zed 17 Posted March 7 On 3/6/2025 at 11:36 PM, Anders Melander said: https://joeduffyblog.com/2006/08/22/priorityinduced-starvation-why-sleep1-is-better-than-sleep0-and-the-windows-balance-set-manager/ The behavior of the Sleep function was changed in Windows Server 2003, making it safe to use Sleep(0) — unless you're writing code for Windows XP. https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep 1 1 Share this post Link to post
Anders Melander 1951 Posted March 7 On 3/7/2025 at 9:22 AM, zed said: The behavior of the Sleep function was changed in Windows Server 2003, making it safe to use Sleep(0) Thanks. I didn't get that memo 🙂 Share this post Link to post