procedure TTestThread.Execute ;
begin
  FLock.Enter ;
  hEvent:=CreateEvent(nil,True,False,nil);
  if intRun=1 then SetEvent(hEvent) else resetEvent(hEvent);
  if WaitForSingleobject(hEvent,INFINITE)=WAIT_OBJECT_0 then  Synchronize(ShowMsg);
  intRun:=0;
  FLock.Leave ;
end;
 
procedure TTestThread.ShowMsg ;
begin
  Form1.Memo1.Lines.Add(FData);
end;
 
intRun,hEvent均是TTestThread的私有成员
 
线程初始时处于不发信号的状态并加入线程池中
 
通过
 
TTestThread(ThreadPooler.FActiveList.Items[i]).intRun:=1;
来设置线程池内线程的发信号状态
 
问题1:照理该线程处于发信号状态时TTestThread.Execute因
 
改继续往下走啊,也就是MEMO1上显示FDATA,而该段程序却并
 
非如此,不知为何。
 
问题2:可以通过再次CREATE执行EXECUTE,但这样的效率很低
 
,速度慢了很多,请问有什么办法可以不通过再次CREATE而执
 
行EXECUTE(此时处于发信号状态),线程创建时是inherited 
 
Create(false), 所以也不能用resume.

解决方案 »

  1.   

    在这里waitforsingleobject(hevent,infinite)你是否把创建事件的HANDLE与线程的HANDLE弄混了.这个hevent是不是应该由createthread来得到!个人意见:)
      

  2.   

    对不起我刚才查了一些资料.在crateevent的时候.第三个参数如果设置为True则初始为有信号那么线程可直接执行?
      

  3.   

    procedure TTestThread.Execute ;
    begin
      while not Terminated do
      begin
        if WaitForSingleObject(FEvent.Handle, INFINITE) = WAIT_OBJECT_0 then
        try
          FLock.Enter;
          //dosomething
        finally
          FLock.Leave;
        end;
      end;
    end;