Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a simple loop inside of Synchronize?

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);
like image 422
Bill Avatar asked Jul 12 '13 19:07

Bill


People also ask

Can we use static and synchronized together?

Static Synchronized method is also a method of synchronizing a method in java such that no two threads can act simultaneously static upon the synchronized method. The only difference is by using Static Synchronized.

Can we synchronize the run method?

Yes, we can synchronize a run() method in Java, but it is not required because this method has been executed by a single thread only. Hence synchronization is not needed for the run() method.

How can we avoid synchronization problem in threads?

One way to avoid synchronization is to make every resource immutable which would be accessed by multiple threads. Making class/object immutable makes sure its threadsafe.


1 Answers

You simply need to declare a local variable for your loop counter:

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
);

A loop variable must be local to the procedure in which the loop appears. In your code you had declared the variable as a local in a different procedure. And hence the compilation error.

like image 58
David Heffernan Avatar answered Sep 28 '22 08:09

David Heffernan