Recent

Author Topic: [solved] Sleep in a thread  (Read 2362 times)

dbannon

  • Hero Member
  • *****
  • Posts: 3374
    • tomboy-ng, a rewrite of the classic Tomboy
[solved] Sleep in a thread
« on: October 27, 2023, 01:33:46 pm »
Clearly, its a bad thing to do any sort of sizable sleep() in an LCL app's main thread but I would like to call sleep() in a 'background' thread for, perhaps, two seconds at a time while that thread waits to get a lock before proceeds to do some file i/o.

Seems reasonable to me and far less overhead than creating a timer. Am I sounding reasonable ?

Davo
« Last Edit: October 27, 2023, 11:41:54 pm by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Dzandaa

  • Sr. Member
  • ****
  • Posts: 452
  • From C# to Lazarus
Re: Sleep in a thread
« Reply #1 on: October 27, 2023, 02:57:44 pm »
Hi,

Just for info, I use this procedure for Sleep that enable Messages process:

Code: Pascal  [Select][+][-]
  1. // ********************************************
  2. // ***** Real Sleep with Messages Process         *****
  3. // ********************************************
  4. procedure RealSleep(DelayTickCount: QWORD);
  5. var
  6.  StartTickCount : QWORD;
  7. begin
  8.  StartTickCount := GetTickCount64;
  9.  while (GetTickCount64 < StartTickCount + DelayTickCount) and (not Application.Terminated) do
  10.  begin
  11.   Application.ProcessMessages;
  12.   Sleep(10);
  13.  end;
  14. end;
  15.  

B->
Regards,
Dzandaa

rvk

  • Hero Member
  • *****
  • Posts: 6760
Re: Sleep in a thread
« Reply #2 on: October 27, 2023, 03:20:30 pm »
Just for info, I use this procedure for Sleep that enable Messages process:
This is not needed in a thread. When you sleep() in a thread, the main thread still runs and it messages get processed.

Only when you want to sleep in the main thread you would want the Application.ProcessMessages with a small sleep() proc.

Seems reasonable to me and far less overhead than creating a timer. Am I sounding reasonable ?
I'm not sure if the TTimer itself adds much overhead.
It does however pull the file i/o (opening a locked file) into the foreground/main thread (i.e. the TTimer procedure) and that might be undesirable. That depends on your applications.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8429
Re: Sleep in a thread
« Reply #3 on: October 27, 2023, 04:01:04 pm »
Clearly, its a bad thing to do any sort of sizable sleep() in an LCL app's main thread but I would like to call sleep() in a 'background' thread for, perhaps, two seconds at a time while that thread waits to get a lock before proceeds to do some file i/o.

Seems reasonable to me and far less overhead than creating a timer. Am I sounding reasonable ?

Yes, entirely IMO. There's often "purer" ways of doing it based on events etc., and in your case there might be a way of integrating your OS's File Alteration Monitor with events, but on occasion the "obvious" way and in particular the "obvious to a maintainer" way has a great deal going for it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dbannon

  • Hero Member
  • *****
  • Posts: 3374
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Sleep in a thread
« Reply #4 on: October 27, 2023, 11:41:27 pm »
Thanks folks, I will now do it with a clear conscious.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018