📄 sendthread1.cpp
字号:
// SendThread1.cpp : 实现文件
//
#include "stdafx.h"
#include "MyThread.h"
#include "SendThread1.h"
// SendThread1
IMPLEMENT_DYNCREATE(SendThread1, CWinThread)
SendThread1::SendThread1()
{
m_stop_state=false;
m_active1=false;
m_pDlg=NULL;
}
SendThread1::~SendThread1()
{
}
BOOL SendThread1::InitInstance()
{
// TODO: 在此执行任意逐线程初始化
return TRUE;
}
int SendThread1::ExitInstance()
{
// TODO: 在此执行任意逐线程清理
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(SendThread1, CWinThread)
END_MESSAGE_MAP()
void SendThread1::GetOwner(CMyThreadDlg *m_pOwner,Thread3* pThread3)
{
m_pDlg=m_pOwner;
m_thread3=pThread3;
}
// SendThread1 消息处理程序
int SendThread1::Run()
{
CString command,nowtime;
int m_second=0, haveDate=0,send_speed=0,isdbConnect=0;
db_user=m_pDlg->db_user;
db_password=m_pDlg->db_password;
db_server=m_pDlg->db_server;
if(!ConnectDB())return FALSE;
try
{
m_recordset.CreateInstance("ADODB.Recordset");
}
catch(_com_error e)
{
m_pDlg->AddString("SendThread1创建数据集失败!");
return FALSE;
}
//////////////////////
int res=-1;
while(!m_active1)
//while(!m_stop_state)
{
m_pDlg->g_cs.Lock();
m_active1=m_pDlg->m_active;
m_pDlg->g_cs.Unlock();
if(!m_stop_state) Sleep(1000);
else break;
}
m_pDlg->AddString("SendThread1 开始");
switch(m_pDlg->send_speed)
{
case 1:
send_speed=3500;//8/s
break;
case 2:
send_speed=5000;//6/s
break;
case 3:
send_speed=7000;//4/s
break;
default:
send_speed=3500;//8/s
break;
}
while(!m_stop_state)
{
try
{
m_pDlg->g_cs.Lock();
m_active1=m_pDlg->db_active;
m_pDlg->g_cs.Unlock();
if (!m_active1){Sleep(6000);continue;}//数据库连接中断
if(isdbConnect==1)//有误重连
{
ConnectDB();
isdbConnect=0;
}
if(haveDate==0)
{
//hr=m_recordset->Open("SELECT convert(varchar(20),getdate()) nowtime,datepart(second,getdate()) m_second ", _variant_t((IDispatch*)(m_pDlg->m_connection),true),adOpenStatic,adLockOptimistic,adCmdText);
m_recordset=m_connection->Execute("SELECT convert(varchar(20),getdate()) nowtime,datepart(second,getdate()) m_second ",&RecordsAffected,adCmdText);
if(!m_recordset->adoEOF)
{
nowtime.Empty();
m_second=0;
if(m_recordset->GetCollect("nowtime").vt == VT_NULL){m_recordset->Close(); continue;}
nowtime=m_recordset->GetCollect("nowtime").bstrVal;
if(m_recordset->GetCollect("m_second").vt != VT_NULL)
m_second=m_recordset->GetCollect("m_second").lVal;
m_recordset->Close();
}
else {m_recordset->Close(); continue;}
}
m_pDlg->g_cs.Lock();
m_active1=m_pDlg->m_active;
m_pDlg->g_cs.Unlock();
if (!m_active1){Sleep(5000);continue;}//网关连接中断
command.Empty();
command.Format("SELECT *FROM T_gwdx_sendTemp where time< dateadd(second,%d,convert(datetime,'%s'))",m_second,nowtime);
m_recordset=m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
if(!m_recordset->adoEOF)/// m_pDlg->AddString("读取数据ok");
{
haveDate=1;
m_recordset->Close();
command.Empty();
command.Format("insert into T_gwdx_send_xlt SELECT nNeedReply,msgtype,contentType,service_id,service,feetype,fixedfee,feecode,srctermid,chargetermid,desttermid,msgcontent,getdate() FROM T_gwdx_sendTemp where time< dateadd(second,%d,convert(datetime,'%s'))",m_second,nowtime);
//hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)(m_pDlg->m_connection),true),adOpenStatic,adLockOptimistic,adCmdText);
m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
command.Empty();
command.Format("delete from T_gwdx_sendTemp where time< dateadd(second,%d,convert(datetime,'%s'))",m_second,nowtime);
m_connection->Execute(command.GetBuffer(0),&RecordsAffected,adCmdText);
//hr=m_recordset->Open(command.GetBuffer(0), _variant_t((IDispatch*)(m_pDlg->m_connection),true),adOpenStatic,adLockOptimistic,adCmdText);
Sleep(send_speed);
}//有数据
else
{
m_recordset->Close();
if(haveDate==1)Sleep(500);else Sleep(8000);
haveDate=0;
}
}//end try
catch(_com_error e)
{
m_pDlg->AddString("SendThread1操作数据库失败!");
if(m_connection->State)m_connection->Close();
if(m_recordset->State)m_recordset->Close();
Sleep(5000);
isdbConnect=1;
//return FALSE;
}
}//线程while
PreStop();
m_pDlg->AddString("Sendthread1退出");
//m_thread3->m_stop_state=true;
//m_thread3->ResumeThread();
return 0;
}
BOOL SendThread1::ConnectDB()
{
try
{
hr=m_connection.CreateInstance("ADODB.Connection");
if(SUCCEEDED(hr))
{
//hr=m_connection->Open("ivsp","ivsp","suntek",adModeUnknown);
hr=m_connection->Open(db_server.GetBuffer(0),db_user.GetBuffer(0),db_password.GetBuffer(0),adModeUnknown);
m_connection->Execute("set rowcount 30",&RecordsAffected,adCmdText);
m_pDlg->AddString("SendThread1连接数据库成功");
}
else
{
m_pDlg->AddString("SendThread1连接数据库失败!");
}
}
catch(_com_error e)
{
m_pDlg->AddString("SendThread1连接数据库失败!");
return false;
}
return true;
}
BOOL SendThread1::PreStop()
{
try
{
if (m_connection->State) m_connection->Close();
if(m_recordset->State)m_recordset->Close();
m_connection=NULL;
m_recordset=NULL;
m_pDlg->AddString("SendThread1关闭数据库");
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("thread1关闭数据库失败!\r\n错误信息:%s",e.ErrorMessage());
m_pDlg->AddString(errormessage.GetBuffer(0));
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -