Strange behaviour of function Sleep() used in repeat until in Delphi

delphi, delphi-7, repeat, sleep

Solution

Don't use `Sleep` function in GUI thread because you are blocking normal message processing and your application behaves strangely.

It is not clear why you use `Sleep` in your code. Probably you should update labels from `OnTimer` event handler of `TTimer` component instead of using a loop.

Problem

I have function which is reaction on button click. When I click on the button it should start repeat and write values form an array and show them in labels on main form. Problem is with function sleep - there is some bug or something, cause when I click on the button it waits quite a long time and then it finaly start the action but very quickly. Let's look at my code. Thx for advices. ``` procedure TForm1.ButtonMereniClick(Sender: TObject); var iterator: Integer; begin iterator := 1; repeat //write some values stored int arrays to labels on form LabelTeplota.Caption:='Teplota: '+FloatToStr(poleTeplota[iterator]); LabelVlhkost.Caption:='Vlhkost: '+FloatToStr(poleVlhkost[iterator]); LabelTlak.Caption:='Atmosférický tlak: '+FloatToStr(poleTlak[iterator]); LabelRychlost.Caption:='Rychlost větru: '+FloatToStr(poleRychlost[iterator]); LabelRychlost.Caption:='Rychlost větru: '+FloatToStr(poleRychlost[iterator]); LabelIterator.Caption:='iterator: '+IntToStr(iterator); Sleep(500);//should be 5000 Inc(iterator); until iterator = 20; end; ```

Original source