还是多线程通信问题,请求妖哥来解答
主要代码如下:Unit_Main.cpp ,Unit_Ping.cpp ,Unit_ADOQ.cpp ,
还有一个Unit_DM就没有写了,里面就是一个ado连接
- C/C++ code
************************************************************************Unit_Main.cpp文件//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit_Main.h"#include "Unit_DM.h"#include "Unit_Ping.h"#include "Unit_ADOQ.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ thread_ADOQ = new TADOQThread(false,PingResultMemo); thread_Ping=new TPingThread(true, "192.168.1.1", PingResultMemo, thread_ADOQ->ThreadID); thread_Ping->Resume(); }//---------------------------------------Unit_Main.h文件里声明了: TPingThread * thread_Ping; TADOQThread * thread_ADOQ; TMemo *PingResultMemo;************************************************************************Unit_Ping.cpp文件//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit_Ping.h"#include "Unit_DM.h"#define MY_MSG WM_USER+100#pragma package(smart_init)//---------------------------------------// 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 TPingThread::UpdateCaption()// {// Form1->Caption = "Updated in a thread";// }//---------------------------------------__fastcall TPingThread::TPingThread(bool CreateSuspended,AnsiString sIp,TMemo *ResultMemo,int thread_ADOQ_ID) : TThread(CreateSuspended){ IP = sIp; BMemo = ResultMemo; Interval = 10000; ADOQ_ID = thread_ADOQ_ID; ICMP_Ping= new TIdIcmpClient(NULL); ICMP_Ping->BufferSize = 8192; ICMP_Ping->Port = 0; ICMP_Ping->Protocol = 1; ICMP_Ping->ReceiveTimeout = 5000; ICMP_Ping->Host = IP; ICMP_Ping->OnReply = ICMP_PingReply;}//---------------------------------------__fastcall TPingThread::~TPingThread(){ delete ICMP_Ping;}//----------------------------------------void __fastcall TPingThread::Execute(){ //---- Place thread code here ---- while( !Suspended ) { try { ICMP_Ping->Ping(); Application->ProcessMessages(); Sleep(Interval); } catch(...) { Suspended = true; } }}//---------------------------------------void __fastcall TPingThread::ICMP_PingReply(TComponent *ASender, const TReplyStatus &AReplyStatus){ AnsiString S ; if( AReplyStatus.ReplyStatusType == rsEcho ) { BMemo->Lines->Add( Now().CurrentDateTime().FormatString("yyyyMMdd HH:mm:ss") + AnsiString(";") + IP + AnsiString(";Ping 成功!") ); S = AnsiString("update hx_monitor set Ping文字状态=") + AnsiString("'Ping 成功!'") + AnsiString(" where IP地址=") + AnsiString("'") + IP + AnsiString("'") ; if( PostThreadMessage(ADOQ_ID,MY_MSG,(WPARAM)S.c_str(),0) ) { BMemo->Lines->Add( Now().CurrentDateTime().FormatString("yyyyMMdd HH:mm:ss") + AnsiString(";") + IP + AnsiString("发送消息成功!线程ID:") + AnsiString(ADOQ_ID) ); } if( !PostThreadMessage(ADOQ_ID,MY_MSG,(WPARAM)S.c_str(),0) ) { BMemo->Lines->Add( Now().CurrentDateTime().FormatString("yyyyMMdd HH:mm:ss") + AnsiString(";") + IP + AnsiString("发送消息不成功!错误号:") + AnsiString(GetLastError()) + AnsiString(";线程ID:") + AnsiString(ADOQ_ID) ); } }}//---------------------------------------Unit_Ping.h文件声明了: TIdIcmpClient *ICMP_Ping; TMemo * BMemo; AnsiString IP; int Interval; int ADOQ_ID;************************************************************************Unit_ADOQ.cpp文件//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit_ADOQ.h"#include "Unit_DM.h"#define MY_MSG WM_USER+100#pragma package(smart_init)//---------------------------------------// 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 TADOQThread::UpdateCaption()// {// Form1->Caption = "Updated in a thread";// }//---------------------------------------__fastcall TADOQThread::TADOQThread(bool CreateSuspended,TMemo *ResultMemo) : TThread(CreateSuspended){ CMemo = ResultMemo; //PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);}//---------------------------------------void __fastcall TADOQThread::Execute(){ //---- Place thread code here ---- CoInitialize(NULL); ADOQ_update_status = new TADOQuery(NULL); ADOQ_update_status->Connection = DM->ADOConn_226_11;//Unit_DM里面的连接 while( !Suspended ) { GetMessage(&msg,0,0,0); switch(msg.message) { case MY_MSG: char * pInfo= (char *)msg.wParam; CMemo->Lines->Add( Now().CurrentDateTime().FormatString("yyyyMMdd HH:mm:ss") + AnsiString("接收到的消息如下:") + AnsiString(pInfo) ); if( AnsiString(pInfo).Trim() != "" ) { ADOQ_update_status->SQL->Clear(); ADOQ_update_status->SQL->Add( AnsiString(pInfo) ); ADOQ_update_status->ExecSQL(); } } } delete ADOQ_update_status; CoUninitialize();}//---------------------------------------Unit_ADOQ.h声明了: TMemo * CMemo; MSG msg; TADOQuery * ADOQ_update_status;************************************************************************
现在的问题是:
第一次基本成功,可以收到消息,但是第二次开始就不行了,
总是PostThreadMessage 返回1444错误,该怎么办呢?
麻烦妖哥看看,谢谢!
分不多了,不好意思!
-------怎么现在不能加分了呢?-------
[解决办法]
你把ICMP_PingReply()函数中
if( PostThreadMessage(ADOQ_ID,MY_MSG,0,(LPARAM)pInfo) )
{。。。}
if( !PostThreadMessage(ADOQ_ID,MY_MSG,0,(LPARAM)pInfo) )
{。。。}
修改为
if( PostThreadMessage(ADOQ_ID,MY_MSG,0,(LPARAM)pInfo) )
{...}
else
{...}