Recent

Author Topic: Thread and Timer  (Read 483 times)

superc

  • Full Member
  • ***
  • Posts: 193
Thread and Timer
« on: July 04, 2022, 09:05:37 am »
Hello,

now I am using a procedure into TThread to perform a series of operations with a specific cadence; the use of a thread inside the timer sounds bad to me even if it works: what would you use instead of the timer?

Thanks in advance.

jamie

  • Hero Member
  • *****
  • Posts: 4748
Re: Thread and Timer
« Reply #1 on: July 04, 2022, 12:16:01 pm »
use the timer to start or stop the thread.

This is assumed that the timer is from the main thread.


The only true wisdom is knowing you know nothing

y.ivanov

  • Sr. Member
  • ****
  • Posts: 497
Re: Thread and Timer
« Reply #2 on: July 04, 2022, 01:11:23 pm »
Hello,

now I am using a procedure into TThread to perform a series of operations with a specific cadence; the use of a thread inside the timer sounds bad to me even if it works: what would you use instead of the timer?

Thanks in advance.

Since it is in a separate thread, you can just "sleep" for a given period, the only small complication comes if you want the thread to respond more quickly in the case of termination:

Code: Pascal  [Select][+][-]
  1. procedure TYourThread.Execute;
  2. var
  3.   MillisRem: Cardinal;
  4. begin
  5.   while not Terminated do
  6.   begin
  7.  
  8.     DoYourStuffHere; // <----
  9.  
  10.     MillisRem := YOUR_INTERVAL_IN_MILLISECONDS;
  11.     while not Terminated and (MillisRem > 0) do
  12.     begin
  13.       Sleep(Min(100, MillisRem));
  14.       Dec(MillisRem, Min(100, MillisRem));
  15.     end;
  16.   end;
  17. end;

Or if you want to include the processing to be included into the interval:

Code: Pascal  [Select][+][-]
  1. procedure TYourThread.Execute;
  2. var
  3.   EndTicks: QWord;
  4. begin
  5.   while not Terminated do
  6.   begin
  7.     EndTicks := GetTickCount64 + YOUR_INTERVAL_IN_MILLISECONDS;
  8.  
  9.     DoYourStuffHere; // <----
  10.  
  11.     while not Terminated and (EndTicks > GetTickCount64) do
  12.       Sleep(Min(100, EndTicks - GetTickCount64));
  13. end;
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 4523
  • Compiler Developer
Re: Thread and Timer
« Reply #3 on: July 04, 2022, 01:11:52 pm »
now I am using a procedure into TThread to perform a series of operations with a specific cadence; the use of a thread inside the timer sounds bad to me even if it works: what would you use instead of the timer?

What do you mean with use of a thread inside the timer? Do you mean you start the thread inside the timer? If so, that is no problem and fine (though you should check that you don't start the thread twice, e.g. by disabling the timer and only enabling it when the thread has finished). The other way around would be problematic however...

superc

  • Full Member
  • ***
  • Posts: 193
Re: Thread and Timer
« Reply #4 on: July 04, 2022, 04:21:05 pm »
yes the thread is started by the timer in the main thread: my question was if it was to be considered a good practice

440bx

  • Hero Member
  • *****
  • Posts: 3011
Re: Thread and Timer
« Reply #5 on: July 04, 2022, 10:01:21 pm »
yes the thread is started by the timer in the main thread: my question was if it was to be considered a good practice
I would consider it bad practice, probably even very bad practice. 

The reason is, a timer shouldn't be allocating O/S resources because if it does, it is quite unclear what other part of the program is responsible for freeing those resources.  A thread is usually created when some ad-hoc task needs to be accomplished, if the task repeats with the passage of time then, there should be a permanent piece of code in the program that takes of it and the timer can simply execute that piece of code or activate a mechanism in the program that queues the execution of the code  (e.g, resume a thread that will check the queue and determines what has to be done.)

Creating a thread in a timer means that there could be dozens, if not more, of threads running and they'd have to end themselves.  Creating threads is not as expensive as creating a process but, it is _not_ cheap.  Code in timers should be low overhead, fast execution, otherwise they could end up stepping on their own toes.

Creating a thread in a timer is, at the very least, problematic.

HTH.
« Last Edit: July 04, 2022, 10:03:17 pm by 440bx »
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

mercurhyo

  • Full Member
  • ***
  • Posts: 219
Re: Thread and Timer
« Reply #6 on: July 04, 2022, 11:26:51 pm »
TThreadTimer lives somewhere in pascal

https://www.programmerall.com/article/1914858525/

it may inspires...
DEO MERCHVRIO - Linux, Win10pro - Ryzen9XT 24threads + Geforce Rtx 3080SUPRIM
god of financial gain, commerce, eloquence (and thus poetry), messages, communication (including divination), travelers, boundaries, luck, trickery and thieves; he also serves as the guide of souls to the underworld

 

TinyPortal © 2005-2018