/////////////////////////
//TheApp.h
class CMyApp:public CWinApp
{
.....
CEvent TheEvent;
static UINT ThreadFunction (LPVOID);
.....
}///////////////////////////
//TheApp.cpp
UINT CMyApp::ThreadFunction(LPVOID pParam)
{
WaitForSingleObject(theEvent.m_hObject,INFINIT);
theEvent. //*************
}编译出错,提示.m_hObject左边的应该是一个类或结构
注意打星星的行,我输入“.”以后,应该显示theEvent的成员,但是没有,如果我把ThreadFunction定义的static去掉,就有了,编译也能通过
我的意思是用一个类成员函数作为线程函数,因此这个函数必须说明为static,然而编译就通不过,另一种方法是将ThreadFunction说明为全局函数,但是我觉得这样有点不符合OO精神,难道前一种安排就没办法实现么?
//TheApp.h
class CMyApp:public CWinApp
{
.....
CEvent TheEvent;
static UINT ThreadFunction (LPVOID);
.....
}///////////////////////////
//TheApp.cpp
UINT CMyApp::ThreadFunction(LPVOID pParam)
{
WaitForSingleObject(theEvent.m_hObject,INFINIT);
theEvent. //*************
}编译出错,提示.m_hObject左边的应该是一个类或结构
注意打星星的行,我输入“.”以后,应该显示theEvent的成员,但是没有,如果我把ThreadFunction定义的static去掉,就有了,编译也能通过
我的意思是用一个类成员函数作为线程函数,因此这个函数必须说明为static,然而编译就通不过,另一种方法是将ThreadFunction说明为全局函数,但是我觉得这样有点不符合OO精神,难道前一种安排就没办法实现么?
--------------------Configuration: ty - Win32 Debug--------------------
Compiling...
ty.cpp
Linking...
ty.obj : error LNK2001: unresolved external symbol "public: static class CEvent CTyApp::ListenerStart" (?ListenerStart@CTyApp@@2VCEvent@@A)
Debug/ty.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...ty.exe - 2 error(s), 0 warning(s)
{
public:
CWinThread * pListener;
static CEvent ListenerStart;
static UINT Listener(LPVOID);
virtual BOOL InitInstance();
protected:
afx_msg BOOL OnInitNet();
............
}BOOL CTyApp::InitInstance()
{
............
ListenerStart.ResetEvent();
...................
pListener=AfxBeginThread(Listener,NULL,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
}
BOOL CTyApp::OnInitNet()
{ ........
ListenerStart.SetEvent();
...............
}UINT CTyApp::Listener(LPVOID pParam)
{
..........
WaitForSingleObject(ListenerStart.m_hObject,INFINITE);
................
}
代码很散,可能问题出在这些方,是不是要导入什么lib文件?
CEvent CTyApp::ListenerStart;
CEvent CTyApp::ListenerStart;啊??
CEvent CTyApp::ListenerStart;
之所以要坚持将线程函数声明为CWinApp类的成员函数,是因为我觉得CTyApp代表一个应用程序对象,从OO的观点来看,应用程序中的线程理应是应用程序类的一部分,即一个成员。
空气的方法应该可行
多谢仙逸先生的Tip,而且你的方法是对的,可是为什么?还请指教!