The second Sleep
is not skipped; you wil see that you are unable to move your Form for 7 seconds.
With Sleep
you make the application unresponsive in such way that it does not get time to update itself. Setting the color of a shape requests the parenting window to paint the shape again (with the new color settings), but these paint-requests are not handled, because you pause the application. Both color changes áre handled, but both paint-requests are packed into one, leaving only the latter to last.
An easy way is to update the Form yourself in between:
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if ShapeGreen.Brush.Color = clLime then
begin
Sleep(4000);
ShapeRed.Brush.Color := clMaroon;
ShapeYellow.Brush.Color := clYellow;
ShapeGreen.Brush.Color := clGreen;
Update;
Sleep(3000);
ShapeRed.Brush.Color := clRed;
ShapeYellow.Brush.Color := clOlive;
ShapeGreen.Brush.Color := clGreen;
end;
end;
But this still makes your application freeze twice. (Note that you cannot do anything in the application during the pauses). A better solution is to make use of a Timer
to eliminate the use of Sleep
all together:
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if ShapeGreen.Brush.Color = clLime then
begin
Timer1.Interval := 4000;
Timer1.Enabled := True;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if ShapeGreen.Brush.Color = clLime then
begin
ShapeRed.Brush.Color := clMaroon;
ShapeYellow.Brush.Color := clYellow;
ShapeGreen.Brush.Color := clGreen;
Timer1.Interval := 3000;
end
else
begin
ShapeRed.Brush.Color := clRed;
ShapeYellow.Brush.Color := clOlive;
Timer1.Enabled := False;
end;
end;
Next step is to make the logic in this code more rubust, for example with (a) variable(s) to decide which colors and intervals should be set.