Jump to content

Recommended Posts

Posted (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 by sfrazor

Share this post


Link to post

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
  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
  On 3/6/2025 at 11:36 PM, Anders Melander said:

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

  • Like 1
  • Thanks 1

Share this post


Link to post
  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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×