高手,给我看看这段代码(关于线程)(100分)

  • 主题发起人 主题发起人 oocoolie
  • 开始时间 开始时间
O

oocoolie

Unregistered / Unconfirmed
GUEST, unregistred user!
调用时使用
Threadmove:=Tmovefile.Create(true);
Threadmove.setVar(source,dest,0);
Threadmove.Resume;
出现abstract error;
另外,想请问waitfor的用法

unit UntObjMoveFile;
interface
uses
Classes,shellapi,Sysutils;
type
TMoveFile = class(TThread)
private
m_source:string;
m_dest:string;
m_Action:integer;
{ Private declarations }
protected
procedure Execute;
override;
public
Procedure MovePro;
procedure setVar(Source,dest:string;Action:integer);
end;

implementation
uses
untservermain;
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure MoveFile.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TMoveFile }


procedure TMoveFile.Execute;
begin
inherited;
if (m_source='') or (m_dest='') then
begin
returnvalue:=0;
exit;
end;
Synchronize(movepro);
end;

procedure TMoveFile.MovePro;
var
shfileopstruct:TshfileOpstruct;
begin
shfileopstruct.Wnd:=FrmserverMain.Handle;
shFileOpstruct.pFrom:=pchar(m_source);
shFileOpstruct.pTo:=pchar(m_dest);
if m_action=0 then
shFileOpstruct.wFunc:=FO_MOVE;
if m_action=1 then
shFileOpstruct.wFunc:=FO_COPY;
shfileopstruct.fFlags:=FOF_NOCONFIRMMKDIR+FOF_NOCONFIRMATION;
if shFileOperation(shfileopstruct)<>0 then
returnvalue:=0
else
returnvalue:=1;
end;

procedure TMoveFile.setVar(Source, dest: string;
Action: integer);
begin
m_source:=trim(source);
m_dest:=(dest);
m_Action:=(Action);
FreeOnTerminate:=true;
end;

end.
 
一个被申明成abstract的类不能被实例化!
 
个人之见,权当见笑:
1、你的thread中没有Create声明;
2、你的SetVar是做什么用的?是给thread传递变量的吗?
如果是,那应该用Create(Source, dest: string;
Action: integer)的形式 ;
3、SetVar不应在thread外调用
 
好了,Oldtiger,按你的意思改在下面这样还是一样的错误。
unit UntObjMoveFile;
interface
uses
Classes,shellapi,Sysutils;
type
TMoveFile = class(TThread)
private
m_source:string;
m_dest:string;
m_Action:integer;
{ Private declarations }
protected
procedure Execute;
override;
public
Procedure MovePro;
constructor create(Source, dest: string;
Action: integer);
end;

implementation
uses
untservermain;
constructor TMoveFile.create(Source, dest: string;
Action: integer);
begin
m_source:=trim(source);
m_dest:=(dest);
m_Action:=(Action);
FreeOnTerminate:=true;
inherited create(false);
end;

procedure TMoveFile.Execute;
begin
inherited;
if (m_source='') or (m_dest='') then
begin
returnvalue:=0;
exit;
end;
Synchronize(movepro);
end;

procedure TMoveFile.MovePro;
var
shfileopstruct:TshfileOpstruct;
begin
shfileopstruct.Wnd:=FrmserverMain.Handle;
shFileOpstruct.pFrom:=pchar(m_source);
shFileOpstruct.pTo:=pchar(m_dest);
if m_action=0 then
shFileOpstruct.wFunc:=FO_MOVE;
if m_action=1 then
shFileOpstruct.wFunc:=FO_COPY;
shfileopstruct.fFlags:=FOF_NOCONFIRMMKDIR+FOF_NOCONFIRMATION;
if shFileOperation(shfileopstruct)<>0 then
returnvalue:=0
else
returnvalue:=1;
end;

end.
 
TThread 类没有实现Execute方法
所以重载Execute方法时不能inherited;
procedure TMoveFile.Execute;
begin
inherited;
//此句有错,应去掉
if (m_source='') or (m_dest='') then
begin
returnvalue:=0;
exit;
end;
Synchronize(movepro);
end
 
我也刚好找到这个原因
谢谢李崇文
 
接受答案了.
 
后退
顶部