dolphin King
asked on
creating threads in delphi
i have a timer that i set to create 5 Threads like following
and here is my thread unit
now my problem is when those Threads created they are not doing the job together only one is start to be executed also it cause a crash to my application
how can i create those 5 Threads 1 by 1 after each thread finished without freezing or crashing the app ?
procedure Tform1.checkaccsesstoserverTimer(Sender: TObject);
begin
checkaccsesstoserver.Enabled := False;
PINGTHRDD := TPINGTHRDD.Create(False, 'value1');
PINGTHRDD := TPINGTHRDD.Create(False, 'Value2');
PINGTHRDD := TPINGTHRDD.Create(False, 'Value3');
PINGTHRDD := TPINGTHRDD.Create(False, 'Value4');
PINGTHRDD := TPINGTHRDD.Create(False, 'Value5');
end;
and here is my thread unit
unit Checkipaddr;
interface
uses
Messages, Windows, SysUtils, dialogs, Classes, Menus, forms, ComOBJ, ShlObj,
SyncObjs, Winsock;
type
TPINGTHRDD = Class(TThread)
private
Fiptoping : String;
procedure DoPING;
{Private}
protected
procedure Execute; override;
{Protected}
Public
constructor Create(CreateSuspended: Boolean; Aiptoping : string);
{Public}
end;
function PortTCP_IsOpen(dwPort : Word; ipAddressStr:AnsiString) : boolean;
var
PINGTHRDD : TPINGTHRDD;
implementation
{ TPINGTHRDD }
function PortTCP_IsOpen(dwPort : Word; ipAddressStr:AnsiString) : boolean;
var
client : sockaddr_in;
sock : Integer;
ret : Integer;
wsdata : WSAData;
begin
Result:=False;
ret := WSAStartup($0002, wsdata); //initiates use of the Winsock DLL
if ret<>0 then exit;
try
client.sin_family := AF_INET; //Set the protocol to use , in this case (IPv4)
client.sin_port := htons(dwPort); //convert to TCP/IP network byte order (big-endian)
client.sin_addr.s_addr := inet_addr(PAnsiChar(ipAddressStr)); //convert to IN_ADDR structure
sock :=socket(AF_INET, SOCK_STREAM, 0); //creates a socket
Result:=connect(sock,client,SizeOf(client))=0; //establishes a connection to a specified socket
finally
WSACleanup;
end;
end;
constructor TPINGTHRDD.Create(CreateSuspended: Boolean; Aiptoping : string);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
Fiptoping := Aiptoping;
end;
procedure TPINGTHRDD.DoPING;
begin
end;
procedure TPINGTHRDD.Execute;
var
I : integer;
begin
for I := 0 to 3 do
begin
if PortTCP_IsOpen(Fipport,Fiptoping) then
begin
//done
end;
end;
end;
end.
now my problem is when those Threads created they are not doing the job together only one is start to be executed also it cause a crash to my application
how can i create those 5 Threads 1 by 1 after each thread finished without freezing or crashing the app ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.