不太熟悉线程,不知道缺少了什么,在构造函数内,一经赋值就出错,高手看看,这是怎么回事.
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;

解决方案 »

  1.   

    这段代码看不出来什么,但构造函数哪里有问题,inherited应该在私有域初始化以后再调用,或者把crate(false)改称create(true),在初始化后再resume,否则线程代码立即执行,某些地方的执行结果可能和预期不一样。
      

  2.   

    inherited  create(false);    
    无论这一句有还是没有,在构造处赋值是都出错,这是怎么回事
      

  3.   

    constructor Create(AInputNum: TArrayStr; AOutCount: Integer);overload;
      

  4.   

    to cdle_net(明浩) :不会吧,构造函数也要不得overload???但是就算是加上overload也行不通
      

  5.   

    type
      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,后面的都没有再显示,这是怎么回事?
      

  6.   

    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{; 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)
    这又是怎么回事?
      

  7.   

    蒙了@~@,
    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没有同步不行。
      

  8.   

    procedure CNMThread.Execute;
    begin
      inherited; //去掉这个,Execute不需要它
      ShowMessage('say2');
    end;
      

  9.   

    结帐的方法:点一楼上面的“管理”,然后,找到“强哥”,Check一下,然后在输入框中填入 20,再回到一楼底部,填密码,点“给分”。