title: Synchronizing Thread for image processing Delphi
body: <p>I must processing one image in main form , but the processing speed is low for inc speed i use thread...</p>
  
  <p>My thread code :</p>
  
  <pre><code>type
    TPaintThread = class(TThread)
      Source,Mask :TBitmap ;
      image : TImage;
      public
         procedure SetAll(src,msk:TBitmap;img:TImage);
      private
       procedure  DoWritePix;
      var
         mBit : TBitmap ;
  
      protected
        procedure Execute; override;
    end;
  
  implementation
  
  procedure TPaintThread.SetAll(src: TBitmap; msk: TBitmap; img: TImage);
  begin
        Source := src ;
        mask := msk ;
        img := img ;
        mBit := TBitmap.Create ;
  end;
  
  procedure TPaintThread.DoWritePix;
  begin
    image.Picture.Bitmap := mBit ;
  end;
  
  procedure TPaintThread.Execute;
  var
      i: Integer;
      j: Integer;
  begin
    mBit.Width := Source.Width ;
    mBit.Height := Source.Height ;
    for i := 1 to Source.Width do
      for j := 1 to Source.Width do
      begin
        // my processing event
      end;
      // result := mBit ;
      // write on form image 
      Synchronize(DoWritePix);
  end;
  </code></pre>
  
  <p>and I'm use it in on <code>timer</code> :</p>
  
  <pre><code>procedure TForm1.tmr1Timer(Sender: TObject);
  var
      pThread  : TPaintThread ;
  begin
    pThread := TPaintThread.Create(True) ;
    pThread.SetAll(MyBmp,mask,img1);
    pThread.Resume ;
    pThread.FreeOnTerminate := True ;
  end;
  </code></pre>
  
  <p>but I have error in <code>DoWritePix</code> in run time :</p>
  
  <pre><code>First chance exception at $005A81DE. Exception class $C0000005 with message 'access violation at 0x005a81de: read of address 0x000001b8'. Process myexe.exe (6032)
  First chance exception at $754E9617. Exception class EAccessViolation with message 'Access violation at address 005A81DE in module 'myexe.exe'. Read of address 000001B8'. Process myexe.exe (6032) 
  </code></pre>
  
  <p>My question: Is that way correct for edit image in main form or not ? if not what is correct way access and write on thread? if yes how I can solve problem ?</p>