我的程序里有线程处理部分,为什么关闭时候总出错呢?》 并且Refreshdata_w.bjdx 偶尔还不执行?我在关闭应用程序时候我不管子线程是否执行完毕,我要直接关闭主程序应该注意什么?
偶尔
unit   xc01; interface uses 
    Classes; type 
    Thread01   =   class(TThread) 
    private 
        {   Private   declarations   } 
    protected 
        procedure   Execute;   override; 
    end; implementation uses   Refreshdata,menu,fbxx; {   Important:   Methods   and   properties   of   objects   in   visual   components   can   only   be 
    used   in   a   method   called   using   Synchronize,   for   example,             Synchronize(UpdateCaption);     and   UpdateCaption   could   look   like,         procedure   Thread01.UpdateCaption; 
        begin 
            Form1.Caption   :=   'Updated   in   a   thread'; 
        end;   } {   Thread01   } procedure   Thread01.Execute; 
begin 
FreeOnTerminate:=true; 
if   bl_xc='gxdata'   then 
      Refreshdata_w.bjdx 
    else 
if   bl_xc='fbxx'   then 
      fbxx_w.appendsave; 
end; end. 

解决方案 »

  1.   

    1.子线程不执行的问题要你自己调试来排除的
    2.主线程可以用下面的方法等到子线程退出了再退出的。
    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    var
      sWord: DWord;
      H: THandle;
    begin
      H := MyThread.Handle;  //子线程的句柄
      if H <= 0 then Exit;
      repeat                 //等待子线程退出
        sWord := MsgWaitForMultipleObjects(1,H,False,INFINITE,QS_ALLINPUT);
        Application.ProcessMessages;
      until (sWord = WAIT_OBJECT_0) or (sWord = WAIT_FAILED);
    end;