📄 index116.htm
字号:
<html>
<style type="text/css"><!--
.p9 { font-family: "宋体"; font-size: 9pt}a {text-transform: none; text-decoration: none;}
a:hover {text-decoration: underline; color: #FF0000;}
--></style>
<body background="../di2001.jpg">
<h3 align="center"><font COLOR="#AOAO99"></font></h3>
<table width="100%" border="1" cellspacing="1">
<tr><td><p align="center"><font color="#FF0000">产生线程的问题?</font></td></tr>
<tr><td><p>
</Br>
我在使用CreateThread时碰到了问题.我想让调用的函数和被调用的函数属于同一个类,结果在我调用CreateThread时得到如下错误:<Br>
</Br>
error C2440: 'type cast' : cannot convert from 'unsigned long (__stdcall Cdmi::*)(void *)' to 'unsigned long (__stdcall *)(void *)'<Br>
</Br>
方法一:<Br>
(1)'unsigned long (__stdcall Cdmi::*)(void *)'是指向Cdmi某个成员函数的指针.<Br>
(2)'unsigned long (__stdcall *)(void *)'仅仅只是一个c形式函数的指针. 编译器无法将(1)转换为(2)是因为c++成员函数取第一个(隐藏)参数"this pointer"作为成员函数,但当是一个静态的成员时则例外.可按如下方法解决.<Br>
</Br>
class XMyThread<Br>
{<Br>
public:<Br>
void StartThread(void);<Br>
virtual UINT ThreadFunction(void);<Br>
static UINT __bogusthreadfunc(LPVOID lpparam);<Br>
};<Br>
</Br>
void XMyThread::StartThread()<Br>
{<Br>
AfxBeginThread(__bogusthreadfunc,this);<Br>
}<Br>
</Br>
UINT XMyThread::ThreadFunction(void)<Br>
{<Br>
//here you do all your real work<Br>
return 0;<Br>
}<Br>
</Br>
UINT XMyThread::__bogusthreadfunc(LPVOID lpparam)<Br>
{<Br>
XMyThread* This = dynamic_cast(lpparam);<Br>
return This->ThreadFunction();<Br>
}<Br>
</Br>
for the sake of clairty, I did not add StopThread and I did not save the<Br>
CWinThread* returned by AfxBeginThread.<Br>
</Br>
If you wanted a thread that does other things, simply derive from XMyThread<Br>
and override ThreadFunction()<Br>
</Br>
example:<Br>
class XAnotherThread : public XMyThread<Br>
{<Br>
virtual UINT ThreadFunction(void);<Br>
};<Br>
</Br>
UINT XAnotherThread :: ThreadFunction(void)<Br>
{<Br>
//do some other work here<Br>
return 0;<Br>
}<Br>
</Br>
方法二:Cdmi::MonitorFiles()是个静态的成员函数.<Br>
</Br>
</Br>
</p></td></tr>
</table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -