我在TThread中使用ipWorks组件TipwIPPort。据我了解,TipwIPPort 是非阻塞的。
我熟悉创建线程,其中Execute方法中的所有处理都是“阻塞”的。
在这种情况下,我需要连接到远程服务器,然后使用TIpwIPPort.DataToSend进行后续调用。我在线程的Execute函数中调用Connect方法。但是永远不会触发 OnConnected事件。
我需要设置哪些参数和属性(例如,传递给构造函数的“CreateSuspended”、FreeOnTerminate值)以便我可以控制何时终止线程。
type TMyThread=class(TThread)
private
IPPort1: TipwIPPort;
procedure IPPort1Connected(Sender: TObject; StatusCode: Integer; const Description: String);
procedure IPPort1DataIn(Sender: TObject; Text: String; EOL: Boolean);
end;
procedure TMyThread.IPPort1Connected(Sender: TObject; StatusCode: Integer; const Description: String);
begin
// never get here
AppendToLog('Status Code in Connect:'+inttostr(StatusCode)+'; Description:'+Description);
if StatusCode = 0 then begin
// send data to server using ipport1.datatosend.....
end;
end;
procedure TMyThread.Execute;
begin
appendtolog('TMyThread.Execute');
IPPort1 := TipwIPPort.Create(nil);
try
With IPPort1 do begin
EOL := #4;
KeepAlive := True;
OnConnected := IPPort1Connected;
OnDataIn := IPPort1DataIn;
end;
IPPort1.Connect('xxx.xxx.xxx.xxx',8888);
appendtolog('done with execute');
finally
end;
end;
procedure TMyThread.IPPort1DataIn(Sender: TObject; Text: String; EOL: Boolean);
begin
if (Pos('keytoendconnection',Text)>0) then begin
ipPort1.Disconnect;
// Terminate the thread and free
end;
end;
procedure TForm1.Button1Click(sender: TObject);
var
myThread;
begin
// what parameters and properties do I need to set to allow me to control when the thread is terminated???
myThread := TMyThread.Create(True);
mSouthernObject.FreeOnTerminate := False;
mSouthernObject.Resume;
end;