请教一个有关线程的问题
小弟刚刚开始学习vc,写了一个基于对话框的网络程序,就是简单的服务器端,接收客户端的文件,直接在对话框内加一些函数,监听一个套结字,用新线程接收客户端文件,用_beginthread,creatthread的时候总是报错说cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void *)'
None of the functions with this name in scope match the target type我看了msdn的说明,不太明白为什么,源代码段如下
void CMydicomtestDlg::Receive(void *pClientSocket)
{
NEWSOCKET *rec=(NEWSOCKET *)pClientSocket;
while (true)
{
int fpid=_open("d:/filename",_O_APPEND|_O_CREAT,_S_IWRITE );
char *lpBuf="";
int nBufLen=1024;
int recvd;
int fd;
for(;;)
{
recvd=recv( rec->s,lpBuf,nBufLen,0 );
if(strcmp(lpBuf,"end")==0)
{
int fpid2;
int fd2;
int recvd2;
fpid2=_open("d:/filename2",_O_APPEND|_O_CREAT,_S_IWRITE );
recvd2=recv( rec->s,lpBuf,nBufLen,0 );
fd2=_write(fpid,lpBuf,recvd);
if(fd==0)
{ break;
}
}
fd=_write(fpid,lpBuf,recvd);
if(fd==0)
{
m_listbox.AddString("C-STROE success");
break;
}
}
}
_endthread();
}
void CMydicomtestDlg::BeginServer()
{
ServerSocket *server= new ServerSocket();
server->Create();
server->Listen();
while(TRUE)
{
NEWSOCKET *rec= new NEWSOCKET ;
if(!server->AcceptClient(* rec))
{
TRACE("接收SOCKET 失败");
break ;
}
else
{
m_listbox.AddString("C-ECHO success");
}
if(_beginthread(Receive,0,rec)== -1)
{
TRACE("开始一个新的客户对话出错");
}
}
_endthread();
}
void CMydicomtestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_beginthread( BeginServer,0,NULL);
m_listbox.AddString("server start");
}
试了用CMydicomtestDlg::BeginServer代替不行
问题点数:50、回复次数:9Top
1 楼hujun614(向2个或2个以上的板块都至少是二星用户的朋友学习)回复于 2002-06-05 20:42:04 得分 5
那就用AfxBeginThread吧,这个函数用起来比较简单Top
2 楼Bind(宁静雪:找到你,绑定你!)(再战江湖)回复于 2002-06-05 20:43:19 得分 20
_beginthread 函数应为全局函数或类的静态函数。
如:
//全局函数
UINT ThreadProc1(LPVOID)
{
....
}
class A
{
static UINT ThreadProc2(LPVOID);//静态函数
};Top
3 楼rovingcat(浪迹猫)回复于 2002-06-05 21:53:57 得分 0
谢谢帮助,能稍微详细一点解释一下全局和静态两种方法么
我试了下声明为静态,就是在对话框类的头文件里声明
class CMydicomtestDlg : public CDialog
{
public:
static UINT BeginServer(LPVOID *empty);
static UINT Receive( LPVOID *pClientSocket);
.....
}
然后在cpp文件里
UINT BeginServer(LPVOID *empty)
{...
}
UINT Receive( LPVOID *pClientSocket)
{
...if(_beginthread(Receive,0,rec)== -1)... ####
}
void CMydicomtestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_beginthread( BeginServer,0,NULL); ******
m_listbox.AddString("server start");
}
结果报错为
cannot convert parameter 1 from 'unsigned int (__cdecl *)(void ** )' to 'void (__cdecl *)(void *)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
以上是对####行的,对******的报错为'_beginthread' : cannot convert parameter 1 from 'unsigned int (void ** )' to 'void (__cdecl *)(void *)'
None of the functions with this name in scope match the target type
为什么两个报错会不同呢,不太明白
Top
4 楼Bind(宁静雪:找到你,绑定你!)(再战江湖)回复于 2002-06-05 21:57:53 得分 0
建议你还是用AfxBeginThread吧。Top
5 楼watchnight(愣头青)回复于 2002-06-05 22:12:31 得分 5
多线程问题就得用到 信号灯 类Top
6 楼rovingcat(浪迹猫)回复于 2002-06-05 22:33:27 得分 0
谢谢,我用了AfxBeginThread( BeginServer, NULL, THREAD_PRIORITY_NORMAL, 0, 0, NULL );
但是还是报错none of the 2 overloads can convert parameter 1 from type 'unsigned int (void ** )'
BeginServer声明为全局UINT BeginServer(lpoid)和对话筐类成员函数都不行
很想解决这个问题,谢谢
Top
7 楼guizicjj(临风)回复于 2002-06-05 23:14:26 得分 20
该是下面这样的吧?
class CMydicomtestDlg : public CDialog
{
public:
static UINT BeginServer(LPVOID *empty);
static UINT Receive( LPVOID *pClientSocket);
.....
}
然后在cpp文件里
UINT BeginServer(LPVOID *empty) //
{
...
if(_beginthread(Receive,0,rec)== -1)...
...
}
UINT Receive( LPVOID *pClientSocket)
{
...
}
void CMydicomtestDlg::OnButton1()
{
// TODO: Add your control notification handler code here
_beginthread( BeginServer,0,NULL);
m_listbox.AddString("server start");
}
Top
8 楼Bind(宁静雪:找到你,绑定你!)(再战江湖)回复于 2002-06-06 09:36:13 得分 0
UINT Receive( LPVOID *pClientSocket)
{
...if(_beginthread(Receive,0,rec)== -1)... ####
}
怎么可以在线程函数里作这种递归调用呢?如果调用函数不能很快结束的话,要不了多久栈就用光了。再说参数也不对。
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
Top
9 楼rovingcat(浪迹猫)回复于 2002-06-07 23:10:52 得分 0
谢谢各位,最后大概按static定义的函数,还是有点小问题,没有在线程里第归调用了,不过用了强制参数类型转换,才能顺利开线程Top




