cannot judge based on the code you presented, but to remove the errors, make the following changes://*.h文件
extern CAdodc g_AdoDC; //ADODC控件
extern CDataGrid g_DataGrid; //DataGrid控件
extern UINT QueryDB(LPVOID pParam);//*.cpp文件
.....
CAdodc g_AdoDC; //ADODC控件
CDataGrid g_DataGrid; //DataGrid控件void CBill121Dlg::DoDataExchange(CDataExchange* pDX)
{
extern CAdodc g_AdoDC; //ADODC控件
extern CDataGrid g_DataGrid; //DataGrid控件
extern UINT QueryDB(LPVOID pParam);//*.cpp文件
.....
CAdodc g_AdoDC; //ADODC控件
CDataGrid g_DataGrid; //DataGrid控件void CBill121Dlg::DoDataExchange(CDataExchange* pDX)
{
程序编译连接都通过了,但运行进入线程QueryDB()时就出错了
UINT QueryDB(LPVOID pParam)
{
g_AdoDC.Refresh(); //运行到这里出错
g_DataGrid.SetRefDataSource(g_AdoDC.GetDSCCursor());
return 0;
}请问各位程序该怎么改
现在程序能运行了,但线程里出错了,请问改如何改。
UINT QueryDB(LPVOID pParam)
{
CBill121Dlg *pDlg = (CBill121Dlg*)pParam; //modify
pDlg->g_AdoDC.Refresh();//modify
pDlg->g_DataGrid.SetRefDataSource(g_AdoDC.GetDSCCursor());//modify
return 0;
}
.....
void CBill121Dlg::OnQuery()
{
.....
g_AdoDC.SetConnectionString(strConnectionString);
g_AdoDC.SetCommandType(1);
g_AdoDC.SetRecordSource(SQL);
AfxBeginThread(QueryDB,this,THREAD_PRIORITY_NORMAL);//modify
}
能否说说具体怎样做呢
程序按你的改了,但同样运行到g_AdoDC.Refresh()就出错了
我将程序改为CCriticalSection g_cs;UINT QueryDB(LPVOID pParam)
{
// g_AdoDC.Refresh();
// g_DataGrid.SetRefDataSource(g_AdoDC.GetDSCCursor());
CBill121Dlg *pDlg = (CBill121Dlg*)pParam;
g_cs.Lock();
pDlg->g_AdoDC.SetConnectionString(pDlg->m_strConnectionString);
pDlg->g_AdoDC.SetCommandType(1);
pDlg->g_AdoDC.Refresh(); //运行到这里出错
pDlg->g_DataGrid.SetRefDataSource(pDlg->g_AdoDC.GetDSCCursor());
g_cs.Unlock();
return 0;
}
错误提示:Unhandled exception in Bill121.exe(KERNEL32.DLL):0xE06D73263:Microsoft C++ Exception
你的
CAdodc g_AdoDC; //ADODC控件
CDataGrid g_DataGrid; //DataGrid控件
定义在何处?
另外CriticalSection的用法不对,主线程中访问共享资源的代码也应放入其中。
将g_AdoDC.Refresh()放在主线程,那么QueryDB(LPVOID pParam)子线程的意义就不大了,因为程序费时间主要在g_AdoDC.Refresh()(这是数据库查询,很费时间)。但如果只有一个主线程的时候,当进行查询的时,程序就不能作其他的操作。我建立QueryDB(LPVOID pParam)子线程主要为了想在程序进行查询的时候,还能响应其他的操作。