读书人

高分线程中创建子线程、静态成员函数的

发布时间: 2012-04-17 15:06:33 作者: rapoo

高分求助线程中创建子线程、静态成员函数的实现等问题
CFilterDlg .h文件:
-----------
class CFilterDlg : public CDialog
{
public:
//--------计算线程------//
static UINT ThreadA (void *param);
CWinThread *pWorkSimulinkThread;

//--------查询线程-------//
static UINT IntCheckThread(void *param);
CWinThread *pCheckThread;

void ShowInfo(CString str);//用于ListBox控件的显示
}

CFilterDlg .cpp文件:
----------
void CFilterDlg::OnOpencheck()
{
//-------创建查询线程-------//
ShowInfo( "正在查询... ",0);
pCheckThread=AfxBeginThread (IntCheckThread,this,THREAD_PRIORITY_TIME_CRITICAL);
bThreadloop=true;
}
//-------查询线程-------//
UINT CFilterDlg::IntCheckThread(void *param)
{
CFilterDlg *dlg=(CFilterDlg *) param;
dlg-> GetParent();

while(bThreadloop)
{
bThreadflag=true;

if(...)//某条件
{
bThreadloop = false;
Threadflag = false;
return 0;
}

else if(...)
{
if(1==Simuflags)
{

ShowInfo( "ok "); //需要静态函数,错误1

//创建线程ThreadA 此处若使用消息传递机制SendMessage()
//则正常
pWorkSimulinkThread=AfxBeginThread(ThreadA ,this,THREAD_PRIORITY_HIGHEST);//这里this使用不当,错误2

}
...
}

bThreadflag=false;

}
return 0;
}

void CFilterDlg::ShowInfo(CString str)//ListBox控件显示
{
m_ListShow.InsertString(m_ListShow.GetCount());
m_ListShow.SetCurSel(m_ListShow.GetCount()-1);
}


====
编译结果是2个错误:
FilterDlg.cpp : error C2352: 'CFilterDlg::ShowInfo ' :illegal call of non-static member function
FilterDlg.cpp : error C2671: 'IntCheckThread ' : static member functions do not have 'this ' pointers




对于第一个错误,如果将其中的ShowInfo修改为static属性,即 将CFilterDlg .h文件中定义的:

void ShowInfo(CString str);
修改为
static void ShowInfo(CString str);

则又会跳出另外的4个错误:
error C2228: left of '.InsertString ' must have class/struct/union type
: error C2228: left of '.GetCount ' must have class/struct/union type
\FilterDlg.cpp(1267) : error C2228: left of '.SetCurSel ' must have class/struct/union type
\FilterDlg.cpp(1267) : error C2228: left of '.GetCount ' must have class/struct/union type

不懂的就是:
1 怎么修改上面子线程IntCheckThread中再创建ThreadA 呢?(除了消息传递机制);
2 如何解决ShowInfo这处错误呢?不改成静态的会报错,改成了静态的又报其他的错误。。。

求达人指教!谢谢!

[解决办法]

CFilterDlg .h文件:
-----------
class CFilterDlg : public CDialog
{
public:
//--------计算线程------//
static UINT ThreadA (void *param);
CWinThread *pWorkSimulinkThread;

//--------查询线程-------//
static UINT IntCheckThread(void *param);
CWinThread *pCheckThread;

void ShowInfo(CString str);//用于ListBox控件的显示
}

CFilterDlg .cpp文件:
----------
void CFilterDlg::OnOpencheck()
{
//-------创建查询线程-------//
ShowInfo( "正在查询... ",0);
pCheckThread=AfxBeginThread (IntCheckThread,this,THREAD_PRIORITY_TIME_CRITICAL);
bThreadloop=true;
}
//-------查询线程-------//
UINT CFilterDlg::IntCheckThread(void *param)
{
CFilterDlg *dlg=(CFilterDlg *) param;
dlg-> GetParent();

while(bThreadloop)
{
bThreadflag=true;

if(...)//某条件
{
bThreadloop = false;
Threadflag = false;
return 0;
}

else if(...)
{
if(1==Simuflags)
{

///////////////////////////////////////////////////
dlg-> ShowInfo( "ok "); //需要静态函数,错误1
//////////////////////////////////////////////////

//创建线程ThreadA 此处若使用消息传递机制SendMessage()
//则正常
///////////////////////////////////////////////////////////////////////
pWorkSimulinkThread=AfxBeginThread(ThreadA ,param,THREAD_PRIORITY_HIGHEST);//这里this使用不当,错误2
//////////////////////////////////////////////////////////////////////
}
...
}

bThreadflag=false;

}
return 0;
}

void CFilterDlg::ShowInfo(CString str)//ListBox控件显示
{
m_ListShow.InsertString(m_ListShow.GetCount());
m_ListShow.SetCurSel(m_ListShow.GetCount()-1);
}
[解决办法]
静态成员函数只能调用静态成员函数。
[解决办法]
你可以这样,把普通的类改成线程驱动的类,用一个静态成员函数做线程入口,然后调用CFilterDlg的一个成员函数作为工作函数,由工作函数完成原线程函数的工作,这样在工作函数中就跟普通的类方法一样编程就行了,可以用this,可以直接访问成员变量等等

-----------
class CFilterDlg : public CDialog
{
public:
//--------计算线程------//
static UINT ThreadA (void *param);
UINT DoThreadA (); // < < < 增加一个函数,做原来ThreadA()的工作
CWinThread *pWorkSimulinkThread;

//--------查询线程-------//


static UINT IntCheckThread(void *param);
UINT DoIntCheckThread(); // < < < 增加一个函数,做原来IntCheckThread()的工作
CWinThread *pCheckThread;

void ShowInfo(CString str); //用于ListBox控件的显示
}


UINT CFilterDlg::ThreadA(void *param)
{
CFilterDlg *dlg=(CFilterDlg *) param;
return dlg-> DoThreadA();
}

UINT CFilterDlg::IntCheckThread(void *param)
{
CFilterDlg *dlg=(CFilterDlg *) param;
return dlg-> DoIntCheckThread();
}

读书人网 >VC/MFC

热点推荐