我在主form中创建了1个附属线程,主form用来接收信号,把信号放入缓冲区。
然后激活辅助线程读取缓冲区,进行信号处理。当信号很多时候,附属线程始终处于运行状态,主form也不能正常关闭了,除非没有信号来了,附属线程也不工作了。
我思考了很久,也找到了解决办法,还没动手作,先来讨教一下大家,希望高人给与指点。我的思路是,附属线程运行后,只要没有引用主form 中的任何对象,完全可以不依赖于form,我在form的close事件中,让form不再接收信号,并且sleep(5000),让最后缓冲区的信号被附属线程拿完处理完毕,自动停止卸载后,form就会正常关闭了。
希望高手给与回复解决方案。谢谢。

解决方案 »

  1.   

    最好而且快、安全、可靠、美观、使用的方法是………设置一个全局变量Theadwork:boolean在线程开始处加上if Theadwork=False then exit;准备启动线程的时候设置Theadwork:=True准备结束线程的时候设置Theadwork:=False   //这样线程就会自杀
      

  2.   

    procedure TMyClientThread.Execute;var
      TheStream: TWinSocketStream;
      buffer: string;
    begin
      { create a TWinSocketStream for reading and writing }
      TheStream := TWinSocketStream.Create(ClientSocket1.Socket, 60000);
      try
        { fetch and process commands until the connection or thread is terminated }
        while (not Terminated) and (ClientSocket1.Active) do
        begin
          try
            GetNextRequest(buffer); { GetNextRequest must be a thread-safe method }        { write the request to the server }
            TheStream.Write(buffer, Length(buffer) + 1);
            { continue the communication (eg read a response from the server) }
            ...
          except
            ifnot(ExceptObject is EAbort) then
              Synchronize(HandleThreadException); { you must write HandleThreadException }
          end;
        end;
      finally
       TheStream.Free;
      end;
    end;
    //MyClientThread.Terminate();结束
      

  3.   

    谢谢上面朋友的帮助。希望继续探讨。
    主线程优先级最高,只要信号来了,附属线程哪怕正在运行,时间片马上又回到主线程。
    我想在想关闭主form的时候,立刻结束附属线程的运行,此时由于主线程优先,也会立刻把时间片抢过来,而执行close方法。但是主线程此时和附属线程的关系是什末呢,它的关闭能自动关闭所有附属线程吗,如何才能达到此目的呢?
      

  4.   

    最好手动关闭所有附属线程,并且要等附属线程关闭结束后,再执行close
      

  5.   

    http://expert.csdn.net/Expert/topic/2178/2178905.xml?temp=.2781641参考一下吧!