就是用户每按一次按钮就生成一个新的线程调用idtcplient控件,这个控件的事件发送完成后就释放该线程。
idtcplient控件的事件:
idtcplient1.Connect;
idtcplient1.WriteLn('helo');
...
idtcplient1.free;

解决方案 »

  1.   

    下面的多线程一运行,就死循环,如何修改?
    如何释放该线程,还有,每个线程都是调用idtcplient1控件,这会不会产生冲突,如何解决?谢谢!!!
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, IdBaseComponent, IdComponent,
      IdTCPConnection, IdTCPClient;type
      TBounceThread = class(TThread)
    Private
      SClient:TIdTCPClient;
      Smail:string;
      Shost:string;
      Sport:integer;
      Shelo:string;
      Sfrom:string;
      Srcpt:string;
    Procedure
       SmtpOut;
        { Private declarations }
    Protected
       procedure Execute;Override;
       public  Constructor Create(Suspended:Boolean;SmtpClient:TIdTCPClient;Smtpmail:string;Smtphost:string;Smtpport:integer;Smtphelo:string;Smtpfrom:string;Smtprcpt:string);
    End;
    implementation
    Procedure TBounceThRead.SmtpOut;
    var
    EmlFile: TFileStream;
    begin
      try
         try
           EmlFile := TFileStream.Create(Smail, fmOpenRead or fmShareDenyWrite);
           SClient.Host:=Shost;
           SClient.Port:=Sport;
           SClient.Connect;
           SClient.SendCmd(Shelo);
           SClient.SendCmd(Sfrom);
           SClient.SendCmd(Srcpt);
           SClient.SendCmd('data');
           SClient.WriteStream(EmlFile,true);
           SClient.Disconnect;
           EmlFile.Free;
         finally
           //发送完毕,释放线程?
         end;
       except
         begin
           //发送不出去,释放线程?
         end;
      end;
    end;procedure TBounceThread.Execute;
      begin
      While Not TerMinated do
        Begin
        Synchronize(SmtpOut); //同步过程
        End;
      End;Constructor TBounceThRead.Create(Suspended:Boolean;SmtpClient:TIdTCPClient;Smtpmail:string;Smtphost:string;Smtpport:integer;Smtphelo:string;Smtpfrom:string;Smtprcpt:string);
      Begin
      inherited Create(Suspended);
      SClient:=SmtpClient;
      Smail:=Smtpmail;
      Shost:=Smtphost;
      Sport:=Smtpport;
      Shelo:=Smtphelo;
      Sfrom:=Smtpfrom;
      Srcpt:=Smtprcpt;
      FreeOnTerminate:=True; //线程对象自动释放(FreeOnTerminate)为True
      End;
    end.Unit1调用多线程:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    TBounceThread.Create(false,IdTCPClient1,'1.eml','192.168.0.1',25,'helo','[email protected]','[email protected]');
    end;