多线程Ping的一种解决方式
ip地址在一个TList中,搞不定了,那位高人有做过了类似的帮帮忙,能提供源代码看看就好了
#include <vcl.h>
#pragma hdrstop
#include "ProbeThread.h"
#include "stdio.h"
#include "probe.h"
#pragma package(smart_init)
TIdIcmpClient *PingClient;
AnsiString LinkState;
AnsiString Host;
int Finished=0;
int nProbeIndex=0;
int nParseIndex=0;
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall ProbeThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall ProbeThread::ProbeThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
FreeOnTerminate = false;
}
//---------------------------------------------------------------------------
void __fastcall ProbeThread::Execute()
{
//---- Place thread code here ----
PingClient=new TIdIcmpClient(NULL);
PingClient->OnReply=ParseReply;
while((!Terminated)&&(!Finished))
{
ProbeIP();
}
delete PingClient;
}
//---------------------------------------------------------------------------
void __fastcall ProbeThread::ProbeIP()
{
PingClient->ReceiveTimeout=2000;
PingClient->BufferSize=100;
Form1->pSection->Acquire();
if(((AnsiString*)Form1->pList->Items[0])->c_str()=="")
{
Finished=1;
return;
}
Host=((AnsiString*)Form1->pList->Items[0])->c_str();
Form1->pList->Delete(0);
PingClient->Host=Host;
PingClient->Ping();
Form1->pSection->Release();
}
//---------------------------------------------------------------------------
voi