请问下面这个代码为什么运行起来。cpu用到了100%需要怎么改。降低cpu利用。
procedure TPainterThread.Execute;
var
  X, Y: Integer;
begin
  Randomize; //初始化 随机数发生器
  repeat
    X := Random (300); //
    Y := Random (Form1.ClientHeight);
    with Form1.Canvas do
    begin
      Lock; //锁定画布
      try
        Pixels [X, Y] := clBlue; //绘制随机的点
      finally
        Unlock;
      end;
    end;
  until Terminated;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Button1.Enabled := False;
  Button2.Enabled := True;
  PT := TPainterThread.Create (False);  // 开始绘图线程
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  PT.Free;
  Button1.Enabled := True;
  Button2.Enabled := False;
end;procedure TForm1.FormMouseDown(
  Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Canvas.Lock; //锁定画布,防止其他线程绘制画布
  try
    Canvas.Pen.Color := clYellow;
    Canvas.Brush.Color := clYellow;
    Canvas.Ellipse (x - 30, y - 30, x + 30, y + 30);
    //在画布上画圆
  finally
    Canvas.Unlock;
  end;
end;

解决方案 »

  1.   

    在until Terminated;前加
    sleep(10)
    即可
      

  2.   

    repeat
        X := Random (300); //
        Y := Random (Form1.ClientHeight);
        with Form1.Canvas do
        begin
          Lock; //锁定画布
          try
            Pixels [X, Y] := clBlue; //绘制随机的点
          finally
            Unlock;
          end;
        end;
        Application.handlemessage; //////////////////////////////////////////////////////
      until Terminated;