Forum > General

Thread and Timer

(1/2) > >>

superc:
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:
use the timer to start or stop the thread.

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


y.ivanov:

--- Quote from: superc 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.

--- End quote ---

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TYourThread.Execute;var  MillisRem: Cardinal;begin  while not Terminated do  begin     DoYourStuffHere; // <----     MillisRem := YOUR_INTERVAL_IN_MILLISECONDS;    while not Terminated and (MillisRem > 0) do    begin      Sleep(Min(100, MillisRem));      Dec(MillisRem, Min(100, MillisRem));    end;  end;end;
Or if you want to include the processing to be included into the interval:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TYourThread.Execute;var  EndTicks: QWord;begin  while not Terminated do  begin    EndTicks := GetTickCount64 + YOUR_INTERVAL_IN_MILLISECONDS;     DoYourStuffHere; // <----     while not Terminated and (EndTicks > GetTickCount64) do      Sleep(Min(100, EndTicks - GetTickCount64));end;

PascalDragon:

--- Quote from: superc on July 04, 2022, 09:05:37 am ---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?

--- End quote ---

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:
yes the thread is started by the timer in the main thread: my question was if it was to be considered a good practice

Navigation

[0] Message Index

[#] Next page

Go to full version