Recent

Author Topic: gui app consume cpu  (Read 582 times)

sydenis

  • Jr. Member
  • **
  • Posts: 50
gui app consume cpu
« on: October 02, 2025, 11:08:41 am »
I have an application that was built in Delphi for win32
In idle time, it takes up 0% of CPU resources
I have now compiled it in Lazarus for win64.
In idle time, it takes up 28% of CPU.

If I uncheck the 'Win32 gui application (-WG)' box in the project options - target system, a console window appears before launching the application.
Then the application works fine and consumes 0% of CPU in idle time.

Is there any way to get rid of the console window at startup, or is there any other way to achieve 0% CPU usage in idle time?

os: Windows10, Lazarus 4.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: gui app consume cpu
« Reply #1 on: October 02, 2025, 11:35:34 am »
A small test shows me a basic application is idle when compiled in release mode (cpu = 0%), but that depends on your specific code it seems?
Can you show us some more code - that compiles - to investigate the problem?
Also, what is the behavior without debugging? i.e. create debug and release modes and compile release.
And run the application outside Lazarus. To me it seems it is either the debugger or something in your code.
Screenshot represents empty one form project. CPU = 0% even running under Lazarus.
Taking the screenshot needs to be started outside of the form screen, otherwise you get paint messages from the form because of cursor movement and thus cpu cycles (~5% on my laptop, same in Delphi12)
« Last Edit: October 02, 2025, 11:55:05 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: gui app consume cpu
« Reply #2 on: October 02, 2025, 11:59:21 am »
Oh, to get rid of the console window add  {$apptype GUI} to your lpr file on Windows. But that is the same as -WG

And when the console has focus it consumes a few CPU cycles, but that is normal behavior: and Delphi behaves the same when a GUI app is compiled with {$apptype console}
« Last Edit: October 02, 2025, 12:11:34 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

sydenis

  • Jr. Member
  • **
  • Posts: 50
Re: gui app consume cpu
« Reply #3 on: October 02, 2025, 12:44:43 pm »
From the beginning, I was running an  exe file outside of Lazarus. It was build in Release mode.Win64
And I also tried inserting {$apptype GUI}, but when using -WG, nothing changes.

I don't know which code makes sense to show, the application is very big.
And most importantly - when building in Delphi or Lazarus with the -WG option disabled, the application behaves as expected and does not load cpu.

Here are the build parameters:

Code: Pascal  [Select][+][-]
  1. C:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
  2. -Twin64
  3. -Px86_64
  4. -MDelphi
  5. -Scgi
  6. -CX
  7. -O3
  8. -XX
  9. -WG
  10. -l
  11. -vewnhibq
  12. -Filib\Release\x86_64-win64
  13. -Fu..\Synapse
  14. -Fu..\JSON
  15. ...
  16. -FuC:\lazarus\components\lazutils\lib\x86_64-win64
  17. -FuC:\lazarus\components\lazdebuggers\lazdebuggerintf\lib\x86_64-win64
  18. -FuC:\lazarus\packager\units\x86_64-win64
  19. -FUlib\Release\x86_64-win64
  20. -FERelease
  21. -oRelease\MYAPP.exe
  22. -dLCL
  23. -dLCLwin32
  24. MYAPP.pas

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12567
  • FPC developer.
Re: gui app consume cpu
« Reply #4 on: October 02, 2025, 12:48:22 pm »
Just pauze the app in the IDE, and see what is executing when it has high CPU?

I'd expect it to be bad handling of some gui/form event that somehow triggers a redraw and then the form event again in some cycle.

sydenis

  • Jr. Member
  • **
  • Posts: 50
Re: gui app consume cpu
« Reply #5 on: October 02, 2025, 12:52:32 pm »
I don't do anything after the launch. There is a connection to mssql and the main form opens, no action takes place. The profiler shows. that there is no background exchange with the database at this time

sydenis

  • Jr. Member
  • **
  • Posts: 50
Re: gui app consume cpu
« Reply #6 on: October 02, 2025, 12:55:36 pm »
I'd expect it to be bad handling of some gui/form event that somehow triggers a redraw and then the form event again in some cycle.
Thank you... I'll try to search

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: gui app consume cpu
« Reply #7 on: October 02, 2025, 12:56:47 pm »
Are you using OnIdle somewhere? Is the done parameter set to true?
Code: Pascal  [Select][+][-]
  1. // connected to Appication.OnIdle somewhere, like in a form.OnCreate
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. begin
  4.   Application.OnIdle:= @DoIdle;
  5. end;
  6.  
  7. procedure TForm1.DoIdle(Sender: TObject; var Done: Boolean);
  8. begin
  9.   Caption := DateTimeToStr(Now);
  10.   Done := false; //keeps running, true stops and only runs when mouse moves over form.
  11. end;        
If done stays false.......
« Last Edit: October 02, 2025, 01:04:51 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

sydenis

  • Jr. Member
  • **
  • Posts: 50
Re: gui app consume cpu
« Reply #8 on: October 02, 2025, 01:00:28 pm »
Are you using OnIdle somewhere? Is the done parameter set to true?
tanks, I'm going to review the code now

Thaddy

  • Hero Member
  • *****
  • Posts: 18524
  • Here stood a man who saw the Elbe and jumped it.
Re: gui app consume cpu
« Reply #9 on: October 02, 2025, 01:06:08 pm »
I have tested my [editted] code above in Delphi too: behavior is the same. CPU = 0% after idle executes once, done = true.
What I did NOT test if done is initialized. (it is a var and that may cause a difference)
« Last Edit: October 02, 2025, 01:09:12 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018