The first AfxBeginThread()
Use the AfxBeginThread() function to create a new thread to execute tasks. The prototype of the worker thread's AfxBeginThread is as follows:
CWinThread* AfxBeginThread(AFX_THREADPROC pfnThreadProc,
LPVOID lParam,
int nPriority = THREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);//Used to create a worker thread
Return value: Returns a pointer to the thread object of the new thread on success, otherwise NULL.
pfnThreadProc : The entry function of the thread, the declaration must be as follows: UINT MyThreadFunction(LPVOID pParam), cannot be set to NULL;
pParam : The parameter passed into the thread, note that its type is: LPVOID, so we can pass a structure into the thread.
nPriority : The priority of the thread, generally set to 0. It has the same priority as the main thread.
nStackSize : Specifies the size of the stack of the newly created thread. If it is 0, the newly created thread has the same size as the main thread The stack of
dwCreateFlags : Specifies what flags the thread has after the thread is created. Two values can be specified:
CREATE_SUSPENDED : After the thread is created, it will be in a suspended state until it is called: ResumeThread
0 : It will start running after the thread is created.
lpSecurityAttrs : Point to a SECURITY_ATTRIBUTES structure, which is used to mark the security of the newly created thread. If it is NULL,
the newly created thread has the same security as the main thread.
If you want to end the thread in the thread, you can Inside call AfxEndThread.
Generally use AfxBeginThread(ThreadProc, this) directly;
Example:
- UINT myproc(LPVOID lParam)
- {
- CITTDlg *pWnd = (CITTDlg *)lParam; //Assign the window pointer to an untyped pointer
- pWnd->KMeansSegment(); //function to execute
- return 1;
- }
- void CITTDlg::KMeansSegment()
- {
- // The main processing function is written here
- }
- void CITTDlg::OnKMeansSegment() //Button click to execute
- {
- AfxBeginThread(myproc, ( LPVOID ) this ); //Start a new thread
- }
Note that the function of the worker thread must be a global function or a static member function, not a normal member function.
The second CreateThread()
NULL, // no security descriptor
0, // default thread stack size
MyThreadProc, // thread function pointer, that is, function name
(LPVOID)&n, // pass parameter
NULL, // no additional property
NULL // no need to get thread number
);
- .h header file
- struct hS
- {
- CString Tmp;
- CTestDlg *hWnd;
- }; //Define a global structure to pass custom messages
- DWORD WINAPI ThreadProc(LPVOIDlpParam); //Thread function declaration, global function
- public:
- CString chtmp;
- struct hS *hTmp;
- protected:
- HANDLE m_hThread; //Thread handle
- CEdit m_Edit ;
- .cpp implementation file
- // thread execution function
- DWORD WINAPI ThreadProc(LPVOID lpParam)
- {
- // write the handler function here
- struct hS *Tmp2;
- Tmp2 = (hS *) lpParam;
- // operate:
- Tmp2->hWnd->m_Edit.SetWindowText( (LPTSTR)Tmp2->Tmp );
- }
- void CTestDlg::OnBnClickedButton1()
- {
- hTmp->Tmp = chtmp;
- hTmp->hWnd = this ; //The key is to pass the this pointer in
- m_hThread =CreateThread(NULL,0,ThreadProc,hTmp,0,NULL);//创建新线程
- CloseHandle(m_hThread );
- }
用CreateThread()函数创建线程将返回一个线程句柄,通过该句柄你可以控制和操作该线程,当你不用时可以一创建该线程后就关闭该句柄,有专门的函CloseHandle()。关闭句柄不代表关闭线程,只是你不能在外部控制该线程(比如,提前结束,更改优先级等)。在线程结束后,系统将自动清理线程资源,但并不自动关闭该句柄,所以线程结束后要记得关闭该句柄。
第三种_beginthread()
void( *start_address )( void * ), //指向新线程调用的函数的起始地址
unsigned stack_size, //堆栈大小,设置0为系统默认值
void *arglist //传递给线程函数的参数,没有则为NULL
);
返回值:
假如成功,函数将会返回一个新线程的句柄,用户可以像这样声明一个句柄变量存储返回值:
HANDLE hStdOut = _beginthread( CheckKey, 0, NULL )。如果失败_beginthread将返回-1。
#include <process.h>
For the thread created by _beginthread(), its thread function is defined as:
void ThreadPro(void * pArguments );