Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it considered bad practice to put an infinite loop inside of TThread.Execute?

i wrote a Thread.descendent class and in the execute method i put a a infinite loop to listen a com event, is considered a bad Threading practice use a infinite loop to do this ? the applications works fine , does not freeze and is allways responsive, i just answer because i want use the best method to threading.

procedure TMyThread.Execute;
begin
    while True and not Terminated do
    begin
     AResult:= FListener.GetResult(Param1,Param2,5000);
      if not VarIsNull(AResult) then
        Synchronize(Process);
    end;
end;
like image 770
Salvador Avatar asked Mar 03 '11 21:03

Salvador


People also ask

Is infinite loop harmful?

No, they're not bad, they're actually useful. It depends whether you left some part of the code that eats up memory as the infinite loop proceeds. Infinite loops are used at almost everything: video games, networking, machine learning, etc.

What happens when you have an infinite while loop?

Infinite While Loop in Python Infinite while loop refers to a while loop where the while condition never becomes false. When a condition never becomes false, the program enters the loop and keeps repeating that same block of code over and over again, and the loop never ends.


1 Answers

The compiler transforms this into:

while not Terminated do

When written out this way I'm sure you'll agree it looks perfectly natural. This is a very common idiom.

like image 177
David Heffernan Avatar answered Nov 14 '22 07:11

David Heffernan