dpr文件begin
   CreateThread(nil, 0, @aa, nil, 0, bb);
   其它代码
    其它代码
     其它代码
end.执行完 '其它代码'后  aa这个线程也会终止 为了让aa不停止 我在end.前加了个 while true do sleep(9999);这样就可以了 可总觉得这样不太好又听说用FreeOnTerminate Terminated WaitFor之类的东西可以办到 试了半天不会玩 这个dpr文件中的begin...end.间的东西也算个线程吧 怎么让它不terminate 怎么让它waitfor呢

解决方案 »

  1.   

    是要aa这个线程不停  dpr文件 begin 
      CreateThread(nil, 0, @aa, nil, 0, bb); 
      其它代码 
        其它代码 
        其它代码 
    end. 执行完这些东西后 aa这个线程不就停止了吗 不能让它停dpr文件 begin 
      CreateThread(nil, 0, @aa, nil, 0, bb); 
      其它代码 
        其它代码 
        其它代码 
       while true do sleep(999999);
    end. 
    我改成这样就能达到目的了,但不想用这样的
      

  2.   


    unit Unit1;interfaceuses
      Classes,SyncObjs;type
      TMyThread = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
      public
        constructor Create(CreateSuspended: Boolean);
        destructor Destroy; override;
      end;implementation
    constructor TMyThread.Create(CreateSuspended: Boolean);
    begin
      FreeOnTerminate:=False;
    end;destructor TMyThread.Destroy;
    begin  inherited;
    end;procedure TMyThread.Execute;
    begin
       While not Terminate do
       begin
         case QuitEvent.WaitFor(1000) of  // until SetEvent set the event to signaled
          wrSignaled, wrAbandoned: Terminate;
       end;
    end;initialization
      QuitEvent := TEvent.Create(nil,true,false,'QuitEvent');finalization
      QuitEvent.Free;end.
      

  3.   

    QuitEvent.WaitFor(1000) 为每隔1000毫秒来处理一下业务
      

  4.   


    var
      h: THANDLE;
    begin 
      h := CreateThread(nil, 0, @aa, nil, 0, bb); 
      其它代码 
        其它代码 
        其它代码   WaitForSingleObject(h, INFINITE);
    end.