多线程Ping的解决

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多线程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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值