不太熟悉线程,不知道缺少了什么,在构造函数内,一经赋值就出错,高手看看,这是怎么回事.
type
CNMThread = class(TThread)
private
FOutCount: integer;
FCNMCount: Integer;
FInputNum: TArrayStr;
FCNMOut: TArrayStr;
{ Private declarations }
protected
procedure Execute; override;
function CNM(AInputNum: TArrayStr; AOutCount:Integer): TArrayStr;
function CNMCount(N,M:Integer):Integer;//计算组合数量
public
constructor Create(AInputNum: TArrayStr; AOutCount: Integer);
end;implementationconstructor CNMThread.Create(AInputNum: TArrayStr; AOutCount: Integer);
begin
inherited create(false);
FOutCount := AOutCount;//到这里就出错
FInputNum := AInputNum;
FCNMCount := CNMCount(High(AInputNum),Foutcount);
end;
procedure CNMThread.Execute;
begin
FCNMOut := CNM(FInputNum,FoutCount);
end;
type
CNMThread = class(TThread)
private
FOutCount: integer;
FCNMCount: Integer;
FInputNum: TArrayStr;
FCNMOut: TArrayStr;
{ Private declarations }
protected
procedure Execute; override;
function CNM(AInputNum: TArrayStr; AOutCount:Integer): TArrayStr;
function CNMCount(N,M:Integer):Integer;//计算组合数量
public
constructor Create(AInputNum: TArrayStr; AOutCount: Integer);
end;implementationconstructor CNMThread.Create(AInputNum: TArrayStr; AOutCount: Integer);
begin
inherited create(false);
FOutCount := AOutCount;//到这里就出错
FInputNum := AInputNum;
FCNMCount := CNMCount(High(AInputNum),Foutcount);
end;
procedure CNMThread.Execute;
begin
FCNMOut := CNM(FInputNum,FoutCount);
end;
无论这一句有还是没有,在构造处赋值是都出错,这是怎么回事
CNMThread = class(TThread)
private
FOutCount: integer;
FCNMCount: Integer;
FInputNum: TArrayStr;
FCNMOut: TArrayStr;
{ Private declarations }
protected
procedure Execute; override;
public
constructor Create({AInputNum: TArrayStr;} AOutCount: Integer);
destructor Destroy; override;
end;implementationuses uChoose;
constructor CNMThread.Create({AInputNum: TArrayStr; }AOutCount: Integer);
begin
inherited create(false);
FOutCount := AOutCount;
ShowMessage('Say1');
end;procedure CNMThread.Execute;
begin
ShowMessage('Say2');
end;destructor CNMThread.Destroy;
begin
ShowMessage('Say3');
inherited Destroy;
end;
前面的问题已经解决,不关overload 的事,是一个字符串数组类型的问题,字符串数组不能传入线程吗?
现在又有了一个新的问题,Execute居然不执行,这个线程只执行显示了Say1,后面的都没有再显示,这是怎么回事?
CNMThread = class(TThread)
private
FOutCount: integer;
FCNMCount: Integer;
FInputNum: TArrayStr;
FCNMOut: TArrayStr;
{ Private declarations }
protected
procedure Execute; override;
function CNM(AInputNum: TArrayStr; AOutCount:Integer{; out Count:Integer}): TArrayStr;
function CNMCount(N,M:Integer):Integer;//计算组合数量
procedure Show();
public
constructor Create({AInputNum: TArrayStr;} AOutCount: Integer);
destructor Destroy; override;
end;implementationuses uChoose;
constructor CNMThread.Create({AInputNum: TArrayStr; }AOutCount: Integer);
begin FreeOnTerminate := True;
inherited create(false);
FOutCount := AOutCount;
ShowMessage('say1');
end;procedure CNMThread.Execute;
begin
inherited;
ShowMessage('say2');
end;destructor CNMThread.Destroy;
begin
ShowMessage('say3');
inherited Destroy;
end;
那个问题也解决了,现在却产生不能预期的错误.First chance exception at $7C812A5B. Exception class EInvalidOperation with message 'Canvas does not allow drawing'. Process CM.exe (4020)
这又是怎么回事?
procedure CNMThread.Execute;
begin
inherited;
ShowMessage('say2');
end;
这里,那个inherited干什么的?execute是个虚函数,inherited什么?
destructor CNMThread.Destroy;
begin
ShowMessage('say3');
inherited Destroy;
end;
为什么要重写析构函数?在onterminate里处理不是更好?
还有那个showmessage能通过编译么?你没有uses dialogs。除了tcanvas类vcl是非线程安全的,showmessage没有同步不行。
begin
inherited; //去掉这个,Execute不需要它
ShowMessage('say2');
end;