How to Use AfxBeginThread in C ++ Part8 MFC (Code instance ),
How to Use AfxBeginThread in C ++ Part8 MFC (code example)
1. The AfxBeginThread parameter.
// Multi-threaded function definition; UINT Thread1 (int pParam) {int nSize = pParam; // your other processing code... Return 0;} // use of multiple threads; int nSize = 50; AfxBeginThread (AFX_THREADPROC) Thread1, (VOID *) nSize, THREAD_PRIORITY_NORMAL, 0, 0, NULL );
AfxBeginThread. If you want to pass multiple parameters or input struct
// Define a global struct; typedef struct SthData {int iStr; int iEnd;} * pSthData; // multithreading function definition; VOID Thread1 (LPVOID lpParam) {// obtain the value in the struct; SthData * pStcSthData = (SthData *) lpParam; int iStr = pStcSthData-> iStr; int iEnd = pStcSthData-> iEnd; // your other processing code... // Remember to delete the pointer of the struct and point to NULL; delete pStcSthData; pStcSthData = NULL;} // use of multiple threads; pSthData pDataValue1 = new SthData; memset (pDataValue1, 0x00, sizeof (SthData); pDataValue1-> iStr = 0; pDataValue1-> iEnd = 10; AfxBeginThread (AFX_THREADPROC) Thread1, pDataValue1, success, 0, 0, NULL );
Note: After the struct is created, you need:
Delete pDataValue1; pDataValue1 = NULL; // prevents wild pointers
Otherwise, the internal data of the struct is garbled during debugging.