清高手指点,在线程的执行过程中怎样调用自定义的函数过程,多谢!!

解决方案 »

  1.   

    如果这个函数用到了全局资源,那么需要用同步的方法,否则直接调用即可。例如:unit1的代码unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationUses
      Unit2;
      
    {$R *.dfm}
    { TForm1 }procedure TForm1.Button1Click(Sender: TObject);
    begin
      zzz.Create(False);
    end;end.unit2的代码unit Unit2;interfaceuses
      Classes,SysUtils;type
      zzz = class(TThread)
      private
        FParm:String;
      private
        Procedure CallProc;
      protected
        procedure Execute; override;
      end;implementationUses
      Unit1;{ zzz }Procedure Output(sText:String);//这个函数使用了外部资源,这里是Form1的资源
    begin
      Form1.Canvas.TextOut(1,1,sText);
    end;Function GetStr:String;//没有用到外部资源的函数
    begin
      Result:=FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',now);
    end;procedure zzz.CallProc;
    begin
      Output(FParm);
    end;procedure zzz.Execute;
    begin
      FParm:=GetStr;//对于没有用到外部资源的函数,直接调用就可以了  Synchronize(CallProc);//对于用到了外部资源的函数,需要用函数Synchronize来调用
    end;end.
      

  2.   

    把你函数的Unit 加到线程里,然后就可以调用了
    uses Unit;
      

  3.   

    谢谢,我会调用了:)
    但是,我是在同一单元里声明的线程,
    //重载构造函数
    constructor Test_thread.create(lb1:TLabel;im1:Timage);
      begin
        //n_no:=no;
        l_b1:=lb1;
        //l_b2:=lb2;
        i_m1:=im1;
        Freeonterminate:=true;
        Inherited create(false);
      end;
    procedure Test_thread.Execute;
    begin
    synchronize(show);
    end;
    //检测线程主程序
    procedure Test_thread.show;
    var
      k,y:integer;begin
    //获得检测编号
      with DataModule5 do
        begin
          ADOQuery1.Close;
          ADOQuery1.SQL.Clear;
          ADOQuery1.SQL.Add('select * from result');
          ADOQuery1.open;
          ADOQuery1.Last;
          testid:=DataModule5.ADOQuery1.FieldByName('TestID').AsString;
          testid:=inttostr(strtoint(trim(testid))+1);
        end;with DataModule5 do
      begin
    //数据库追加纪录
    ADOQuery1.Close;
    ADOQuery1.SQL.Clear;
    ADOQuery1.SQL.Add('select * from result');
    ADOQuery1.open;
    ADOQuery1.Append;//追加检测编号
    ADOQuery1.FieldByName('TestID').AsString:=testid;//追加车牌号码
    ADOQuery1.FieldByName('regino').AsString:='冀D-66666';//检测日期
    ADOQuery1.FieldByName('date').AsDateTime:=strtodate('2004-01-28');//废气检测
      form1.gas;
    //烟度检测
    Form1.smoke;..........end;自定义的过程以gas为例:
      //废气检测
    procedure TForm1.gas;
      begin
      label1.caption:='废气检测';
      showmessage('废气检测');
      sleep(2000);
      ADOQuery1.FieldByName('Hc').AsFloat:=12.4;
      ADOQuery1.FieldByName('Co').AsFloat:=100.4;  
      form1.Gauge1.Progress:=50;
       ...............
      end;在执行线程的过程中ADOQuery1追加(Append)一条记录,但是在调用gas的时候,却出错了,在gas这个过程中“ADOQuery1.FieldByName('Hc').AsFloat:=12.4”,找不到‘Hc',就是追加记录对gas这个过程无效,怎么解决呢?
      

  4.   

    在执行线程的过程中ADOQuery1追加(Append)一条记录,在调用gas的时候就中断了这次追加,那怎样解决呢?