Multithreading Delphi图像处理中的同步线程
我必须处理一个图像的主要形式,但处理速度是低的inc速度我使用线程 我的线程代码:Multithreading Delphi图像处理中的同步线程,multithreading,delphi,synchronization,delphi-xe3,tthread,Multithreading,Delphi,Synchronization,Delphi Xe3,Tthread,我必须处理一个图像的主要形式,但处理速度是低的inc速度我使用线程 我的线程代码: type TPaintThread = class(TThread) Source,Mask :TBitmap ; image : TImage; public procedure SetAll(src,msk:TBitmap;img:TImage); private procedure DoWritePix; var mBit
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;
我在定时器中使用它:
procedure TForm1.tmr1Timer(Sender: TObject);
var
pThread : TPaintThread ;
begin
pThread := TPaintThread.Create(True) ;
pThread.SetAll(MyBmp,mask,img1);
pThread.Resume ;
pThread.FreeOnTerminate := True ;
end;
但是我在运行时的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)
我的问题:这种方式对编辑主窗体中的图像是否正确?若不是,那个么在线程上访问和写入的正确方式是什么?如果是,我如何解决问题?此代码错误:
procedure TPaintThread.SetAll(src: TBitmap; msk: TBitmap; img: TImage);
begin
Source := src ;
mask := msk ;
img := img ; // OOPS!
mBit := TBitmap.Create ;
end;
当你写img:=img代码>你什么都没做–这是不可操作的。你想写:
image := img;
这就是为什么image
在DoWritePix
中是nil
,这解释了访问冲突
当你面对运行时错误时,不要束手无策。在调试器下运行代码,让调试器告诉您哪个变量未初始化。在继续之前设置“FreeOnTerminate”。线程可能在执行命中设置FreeOnTerminate行之前执行并完成。事实上,根本不使用简历,请参阅文档了解原因。。