同步线程进行图像处理Delphi

A1G*_*ard 3 delphi multithreading synchronization tthread delphi-xe3

我必须处理主要形式的一个图像,但处理速度低inc速度我使用线程...

我的线程代码:

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;
Run Code Online (Sandbox Code Playgroud)

我正在使用它timer:

procedure TForm1.tmr1Timer(Sender: TObject);
var
    pThread  : TPaintThread ;
begin
  pThread := TPaintThread.Create(True) ;
  pThread.SetAll(MyBmp,mask,img1);
  pThread.Resume ;
  pThread.FreeOnTerminate := True ;
end;
Run Code Online (Sandbox Code Playgroud)

但我DoWritePix在运行时有错误:

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) 
Run Code Online (Sandbox Code Playgroud)

我的问题:对于主要表单中的编辑图像,这种方式是否正确?如果不是正确的方式访问和写在线程上?如果是的话我怎么解决问题?

Dav*_*nan 8

这段代码错了:

procedure TPaintThread.SetAll(src: TBitmap; msk: TBitmap; img: TImage);
begin
  Source := src ;
  mask := msk ;
  img := img ; // OOPS!
  mBit := TBitmap.Create ;
end;
Run Code Online (Sandbox Code Playgroud)

当你写作img := img;你什么也不做 - 这是一个无操作.你打算写:

image := img;
Run Code Online (Sandbox Code Playgroud)

这就是为什么imagenilDoWritePix这也解释了访问冲突.

遇到运行时错误时不要无助.在调试器下运行代码,让调试器告诉您哪个变量未初始化.