可以在Synchronize中使用一个简单的循环吗?

时间:2013-07-12 19:01:16

标签: delphi delphi-2010

procedure TGridThread.Execute;
var
  i: integer;
  iIEBitmap: TIEBitmap;
  iWidth: integer;
  iHeight: integer;
  iImageCount: integer;
  iArrayOfBitmaps: array of TBitmap;
begin
  inherited;
  { Free the thread onTerminate }
  FreeOnTerminate := True;
  if not Terminated then
    begin    
      if not Terminated then
      begin
        Synchronize(
         procedure
           begin
              iIEBitmap := TIEBitmap.Create(Form1.ImageEnView1.IEBitmap);
              iWidth := Form1.ImageEnMView1.ImageOriginalWidth[0];
              iHeight := (Form1.ImageEnMView1.ImageOriginalHeight[0] + iSpaceBetweenImages) *
              Form1.ImageEnMView1.ImageCount;
              iImageCount := Form1.ImageEnMView1.ImageCount;
            end);

           SetLength(iArrayOfBitmaps, iImageCount);

           Synchronize(
            procedure
            begin
              for i := 0 to iImageCount - 1 do // [DCC Error] Unit1.pas(334): E1019 For loop control variable must be simple local variable
              begin
                iArrayOfBitmaps[i] := Form1.ImageEnMView1.GetBitmap(i);
                { Free the bitmap }
                Form1.ImageEnMView1.ReleaseBitmap(0);
               end;
            end);

1 个答案:

答案 0 :(得分:7)

您只需要为循环计数器声明一个局部变量:

Synchronize(
  procedure
  var
    i: Integer;
  begin
    for i := 0 to iImageCount - 1 do
    begin
      iArrayOfBitmaps[i] := Form1.ImageEnMView1.GetBitmap(i);
      Form1.ImageEnMView1.ReleaseBitmap(0);
    end;
  end
);

循环变量必须是循环出现的过程的本地变量。在您的代码中,您已将变量声明为不同过程中的本地变量。因而编译错误。