我在工作线程中处理ado对象。初始化com+ 都成功就是。而且 多线程中的功能操作都执行成功。
我的主线程中也 ado对象也是在运行之中的那位高手帮解答下。
//启动主线程成功
CWinThread* pThread;
HANDLE pThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)MyThread,NULL,0,NULL);
CloseHandle(hThread);
//
UINT MyThread(LPVOID lpParam)
{
///*
CStdioFile dataStore;CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
BOOL bIsOk = dataStore.Open("f:\\log.txt",
CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate);dataStore.SeekToEnd();
dataStore.WriteString("\n程序开始运行");
//*/
///*
AfxEnableControlContainer(0);if (CoInitialize(0)==S_OK)
{
///*
dataStore.SeekToEnd();
dataStore.WriteString("\n初始化com库成功");
//
}else
{
}*/
_ConnectionPtr m_pConnection;
bool DbMark=DbTheadInit( m_pConnection);//**********就是//******问题所在 m_pConnection一直为空
///*
//多线程调试***********************************************************************
dataStore.SeekToEnd();
if (m_pConnection==NULL)
{CTime time = CTime::GetCurrentTime();
CString m_strTime = CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S");
dataStore.WriteString("\n" + m_strTime + " m_pConnection的值为 空" );
}else
{
dataStore.WriteString("\nm_pConnection 不为空");
}
//*******************************************************************************
//*/}bool DbTheadInit(_ConnectionPtr m_pConnection)
{
//AfxOleInit();//初始化COM库
//_ConnectionPtr m_pConnection;
///*
CStdioFile dataStore;
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
BOOL bIsOk = dataStore.Open("f:\\log1.txt",
CFile::modeCreate | CFile::modeWrite |CFile::modeNoTruncate);
AfxEnableControlContainer(0);
if (CoInitialize(0)==S_OK)
{
dataStore.SeekToEnd();
dataStore.WriteString("\ncom初始化成功" );
}
else
{
dataStore.SeekToEnd();
dataStore.WriteString("\ncom初始化不成功" );}
CString g_Admin,g_Pass,m_strSQL;
//*/
try
{CString OutTime,ConStr,UserName,PassWord;//从配置文件初始化数据连接
::GetPrivateProfileString("AdoInit","ConnectionTimeout","0",OutTime.GetBuffer(MAX_PATH),MAX_PATH,"f://MConfig.ini");
::GetPrivateProfileString("AdoInit","ConnectionStr","",ConStr.GetBuffer(MAX_PATH),MAX_PATH,"f://MConfig.ini");
::GetPrivateProfileString("AdoInit","UserName","",UserName.GetBuffer(MAX_PATH),MAX_PATH,"f://MConfig.ini");
::GetPrivateProfileString("AdoInit","PassWord","",PassWord.GetBuffer(MAX_PATH),MAX_PATH,"f://MConfig.ini");
///*
dataStore.SeekToEnd();
dataStore.WriteString("\nOutTime : " + OutTime
+"\n ConStr : " + ConStr
+ "\n UserName " + UserName
+ "\n PassWord " +PassWord
);//*///根据配置文件中的配置 初始化ado连接
m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
m_pConnection->ConnectionTimeout=3;///设置超时时间为3秒
m_pConnection->Open((_bstr_t)"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data.qy",(_bstr_t)UserName,(_bstr_t)PassWord,0);
}
catch(_com_error e)///捕捉异常
{
m_pConnection=NULL;
CString strComError; \
strComError.Format("错误编号: %08lx\n错误信息: %s\n错误源: %s\n错误描述: %s", \
e.Error(), \
e.ErrorMessage(), \
(LPCSTR) e.Source(), \
(LPCSTR) e.Description()); \
///*
dataStore.SeekToEnd();
dataStore.WriteString("\n" +strComError );
//*/return false ;}///*
dataStore.SeekToEnd();
dataStore.WriteString("\n成功执行" );
//*/
return true;
}没办法,多线程调试 特别麻烦。自己都是在那里不停写日志 才查出m_pConnection 老为空
哎。郁闷其实后面的相关操作都执行完了。
这个是跟数据库打交道的。难道我主线程把一个连接一直打开。还能影响到工作线程
我用的数据库是一个access数据库做ado连接