原型:
unsigned long _beginthread(
void( __cdecl *start_address )( void * ), //你的线程函数
unsigned stack_size, //STACK大小
void *arglist ); //线程函数参数
应用:void NewClient( void *MyID0)
{ .....}
int ThreadNr=11;
_beginthread(
NewClient,
0,
&ThreadNr );OK。
给分了 :)
unsigned long _beginthread(
void( __cdecl *start_address )( void * ), //你的线程函数
unsigned stack_size, //STACK大小
void *arglist ); //线程函数参数
应用:void NewClient( void *MyID0)
{ .....}
int ThreadNr=11;
_beginthread(
NewClient,
0,
&ThreadNr );OK。
给分了 :)
struct DeMakThreadInfo
{
.....
HANDLE m_hEventStartDeMak;
HANDLE m_hEventStopDeMak;
HANDLE m_hEventDeMakThreadKilled;
HANDLE m_hEventDataReceived;
};
UINT DeMakThread(LPVOID pParam);
//线程函数代码
UINT DeMakThread(LPVOID pParam)
{
DeMakThreadInfo* pDeMakInfo=(DeMakThreadInfo*)pParam;
//This thread runs in an infinite loop
//thread sets the "StartDeMak"event.
//The thread wxits the loop only when the main application sets the
//"StopDeMak"event.
while(TRUE)
{
//wait until the main application thread asks this thread
//decision make
if (WaitForSingleObject(pDeMakInfo-〉m_hEventStartDeMak,INFINITE)!=WAIT_OBJECT_0)
break; if(WaitForSingleObject(pDeMakInfo->m_hEventStopDeMak,0)==WAIT_OBJECT_0)
break;
................ }
SetEvent(pDeMakInfo->m_hEventDeMakThreadKilled);
return 0;
}//主线程
//init
m_hEventStartDeMak=CreateEvent(NULL, FALSE, FALSE, NULL); m_hEventStopDeMak=CreateEvent(NULL, FALSE, FALSE, NULL); m_hEventDeMakThreadKilled=CreateEvent(NULL, FALSE, FALSE, NULL)m_pDeMakThreadInfo.m_hEventStartDeMak=m_hEventStartDeMak;
m_pDeMakThreadInfo.m_hEventStopDeMak=m_hEventStopDeMak;
m_pDeMakThreadInfo.m_hEventDeMakThreadKilled=m_hEventDeMakThreadKilled;
//
//start thread
if(m_pDeMakThread=NULL)
{
AfxBeginThread(DeMakThread,&m_pDeMakThreadInfo);
}
ResetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
//stop thread
SetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
WaitForSingleObject(m_hEventDeMakThreadKilled, INFINITE);
m_pDeMakThread = NULL;
///当然最后要clear up
DWORD dwExitCode;
if (m_pDeMakThread != NULL &&
GetExitCodeThread(m_pDeMakThread->m_hThread, &dwExitCode) &&
dwExitCode == STILL_ACTIVE)
{
// Kill the decision make thread by setting the "kill thread" event.
// See comment in Onstop for explanation of the sequence
// of the "stop demak" and "start demak" events.
SetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
WaitForSingleObject(m_hEventDeMakThreadKilled, INFINITE);
}
这是我现在正在做的东西中的一部分,copy过来了,或许还有错误,请指正!
struct DeMakThreadInfo
{
.....
HANDLE m_hEventStartDeMak;
HANDLE m_hEventStopDeMak;
HANDLE m_hEventDeMakThreadKilled;
HANDLE m_hEventDataReceived;
};
UINT DeMakThread(LPVOID pParam);
//线程函数代码
UINT DeMakThread(LPVOID pParam)
{
DeMakThreadInfo* pDeMakInfo=(DeMakThreadInfo*)pParam;
//This thread runs in an infinite loop
//thread sets the "StartDeMak"event.
//The thread wxits the loop only when the main application sets the
//"StopDeMak"event.
while(TRUE)
{
//wait until the main application thread asks this thread
//decision make
if (WaitForSingleObject(pDeMakInfo-〉m_hEventStartDeMak,INFINITE)!=WAIT_OBJECT_0)
break; if(WaitForSingleObject(pDeMakInfo->m_hEventStopDeMak,0)==WAIT_OBJECT_0)
break;
................ }
SetEvent(pDeMakInfo->m_hEventDeMakThreadKilled);
return 0;
}//主线程
//init
m_hEventStartDeMak=CreateEvent(NULL, FALSE, FALSE, NULL); m_hEventStopDeMak=CreateEvent(NULL, FALSE, FALSE, NULL); m_hEventDeMakThreadKilled=CreateEvent(NULL, FALSE, FALSE, NULL)m_pDeMakThreadInfo.m_hEventStartDeMak=m_hEventStartDeMak;
m_pDeMakThreadInfo.m_hEventStopDeMak=m_hEventStopDeMak;
m_pDeMakThreadInfo.m_hEventDeMakThreadKilled=m_hEventDeMakThreadKilled;
//
//start thread
if(m_pDeMakThread=NULL)
{
AfxBeginThread(DeMakThread,&m_pDeMakThreadInfo);
}
ResetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
//stop thread
SetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
WaitForSingleObject(m_hEventDeMakThreadKilled, INFINITE);
m_pDeMakThread = NULL;
///当然最后要clear up
DWORD dwExitCode;
if (m_pDeMakThread != NULL &&
GetExitCodeThread(m_pDeMakThread->m_hThread, &dwExitCode) &&
dwExitCode == STILL_ACTIVE)
{
// Kill the decision make thread by setting the "kill thread" event.
// See comment in Onstop for explanation of the sequence
// of the "stop demak" and "start demak" events.
SetEvent(m_hEventStopDeMak);
SetEvent(m_hEventStartDeMak);
WaitForSingleObject(m_hEventDeMakThreadKilled, INFINITE);
}
这是我现在正在做的东西中的一部分,copy过来了,或许还有错误,请指正!
"先写一个Proc,名字为他的第一个参数"是什么意思?能解释一下吗?
1) 不需要从winthread继承来;
2)
unsigned long _beginthread(
void( __cdecl *start_address )( void * ), //你的线程函数<---这里呀
unsigned stack_size, //STACK大小
void *arglist ); //线程函数参数
去看msdn
:)