主线程等待一个 无阻塞函数 死循环子线程的安全退出

子线程:

procedure TreadThread.Execute;
begin
   inherited;
   FreeOnTerminate := True;
   while not Terminated do begin
        ////dosomething;
      end;
      Sleep(100);
   end;
end;

 

主线程:

var
   H: array[0..0] of Thandle;
begin
      try
         H[0] := RCThread.handle;

         // 安全退出子线程
         RCThread.Terminate;
         if MsgWaitForMultipleObjects(1, H, True, 1000, QS_ALLINPUT) = WAIT_OBJECT_0 then begin
            //线程已经安全退出
         end;
     except
         on e: Exception do
            ShowMessage(e.Message);
      end;
end;

posted @ 2013-04-28 12:47  小天1981  阅读(411)  评论(0编辑  收藏  举报