我試圖將單個線程應用程序轉換爲多線程應用程序。德爾福 - 多線程端口檢查器
基本上,我想每10秒同時檢查50個端口,看他們是否在線或離線。
我使用一個列表框來加載所有的IP和端口(127.0.0.1:50008)他們我解析IP地址和端口號,並使用此功能檢查:
uses idTCPclient;
function IsPortActive(AHost : string; APort : string): boolean;
var
IdTCPClient : TIdTCPClient;
begin
Result := False;
try
IdTCPClient := TIdTCPClient.Create(nil);
try
IdTCPClient.Host := AHost;
IdTCPClient.Port := strtoint(APort);
IdTCPClient.ConnectTimeout:=50;
IdTCPClient.Connect;
Result := True;
finally
IdTCPClient.Free;
end;
except
//Ignore exceptions
end;
end;
下面是該過程開始檢查端口和相應的信號結果:
procedure TForm2.Button1Click(Sender: TObject);
begin
if isportactive('127.0.0.1','50008') then
listbox_online.items.add(ip+''+port)
else
listbox_offline.items.add(ip+''+port);
end;
可能有人請指導我如何將其轉換爲可以接受的IP和端口參數線程?
創建一個主題。創建時傳遞必要的信息。將代碼移入線程的Execute方法。而已。那麼,所有的UI都需要在UI線程上被調用,但我想你知道這一點。 –
你可以請一個例子嗎? – user2858981
不可以。網絡上有無數的例子展示瞭如何創建和執行線程。從一些基礎研究開始。如果您遇到困難,請提出具體問題。 –