Z
zmj94
Unregistered / Unconfirmed
GUEST, unregistred user!
下面的多线程一运行,就死循环,如何修改?
如何释放该线程,还有,每个线程都是调用idtcplient1控件,这会不会产生冲突,如何解决?谢谢!!!
unit Unit2;
interface
uses
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 TerMinateddo
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','user@china.com','user2@mail.com');
end;
如何释放该线程,还有,每个线程都是调用idtcplient1控件,这会不会产生冲突,如何解决?谢谢!!!
unit Unit2;
interface
uses
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 TerMinateddo
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','user@china.com','user2@mail.com');
end;