多线程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();
}
//---------------------------------------------------------------------------
void __fastcall ProbeThread::ParseReply(TComponent *ASender,
const TReplyStatus &AReplyStatus)
{
LinkState="连通";
if(AReplyStatus.BytesReceived!=0)
&nbs

本文探讨了在多线程环境下进行Ping操作时遇到的问题,包括线程冲突和结果准确性。提出了一种解决方案,通过添加额外的变量来协调线程执行,确保每个Ping操作完成后再进行下一次。此外,还提到了使用TStringList代替TList的建议,以简化代码和提高效率。
最低0.47元/天 解锁文章
7327

被折叠的 条评论
为什么被折叠?



