Home > Software engineering >  Consult: AfxBeginThread use CWinThread subclasses to create an interface thread incoming parameters?
Consult: AfxBeginThread use CWinThread subclasses to create an interface thread incoming parameters?

Time:01-14

I am a c + + MFC beginner, in the case of test books, here have a question want to ask the elder

Afxbeginthread MFC multithreading development, used to create a worker thread afxbeginthread (ThreadProc, pParam), apparently can make incoming LPVOID parameter of type pParam,
But creating the user interface thread, AfxBeginThread (RUNTIME_CLASS (CChatThread)), using descendants RUNTIME_CLASS a CWinThread object, there is no heir LPVOID parameters, need the incoming thread parameters should be how to do?




CodePudding user response:

Afraid of trouble with the global variables and parameters

CodePudding user response:

Thank you, a global variable is a method, or know how to pass parameters to the interface thread

CodePudding user response:

This parameter is used to thread stack, but AfxBeginThread (RUNTIME_CLASS (CChatThread)) the form, not provided by you,
You can track thrdcore. CPP and see its implementation, is ultimately by CWinThread defines the structure as a parameter to a local _beginthreadex functions;
 BOOL CWinThread: : CreateThread (DWORD dwCreateFlags, UINT nStackSize, 
LPSECURITY_ATTRIBUTES lpSecurityAttrs)
{
# # ifndef _MT
DwCreateFlags;
NStackSize;
LpSecurityAttrs;

return FALSE;
# the else
ENSURE (m_hThread==NULL);//already created?

//setup startup structure for thread initialization
_AFX_THREAD_STARTUP startup; Memset (& amp; Startup, 0, sizeof (startup));
Startup. PThreadState=AfxGetThreadState ();
Startup. PThread=this;
Startup. HEvent=: : CreateEvent (NULL, TRUE, FALSE, NULL);
Startup. HEvent2=: : CreateEvent (NULL, TRUE, FALSE, NULL);
Startup. DwCreateFlags=dwCreateFlags;
If (startup. HEvent==NULL | | startup. HEvent2==NULL)
{
TRACE (traceAppMsg, 0, "Warning: CreateEvent failed in CWinThread: : CreateThread. \ n");
If (startup hEvent!=NULL)
: : CloseHandle (startup. HEvent);
If (startup hEvent2!=NULL)
: : CloseHandle (startup. HEvent2);
return FALSE;
}

//create the thread (it may or may not start to run)
M_hThread=(HANDLE) (ULONG_PTR) _beginthreadex (lpSecurityAttrs nStackSize,
& _AfxThreadEntry, & amp; Startup, dwCreateFlags | CREATE_SUSPENDED, (UINT *) & amp; M_nThreadID);
If (m_hThread==NULL)
{
: : CloseHandle (startup. HEvent);
: : CloseHandle (startup. HEvent2);
return FALSE;
}

//start the thread just for MFC initialization
VERIFY (ResumeThread ()!=(DWORD) - 1);
VERIFY (: : WaitForSingleObject (startup hEvent, INFINITE)==WAIT_OBJECT_0);
: : CloseHandle (startup. HEvent);

//if created suspended, suspend it until resume thread wakes it up
If (dwCreateFlags & amp; CREATE_SUSPENDED)
VERIFY (: : SuspendThread (m_hThread)!=(DWORD) - 1);

//if the error during startup, shut things down
If (startup. BError)
{
VERIFY (: : WaitForSingleObject (m_hThread, INFINITE)==WAIT_OBJECT_0);
: : CloseHandle (m_hThread);
M_hThread=NULL;
: : CloseHandle (startup. HEvent2);
return FALSE;
}

//allow the thread to continue, once resumed (it may already be resumed)
VERIFY (: : SetEvent (startup. HEvent2));
Return TRUE;
# endif//! _MT
}


From the perspective of the thread itself, if it need some information about the external environment, any way to get the data can be, not can only be created by thread entry functions provide,
User interface thread has its own message pump, so you can be in the thread has been created by hair message give this thread to provide environmental data, interactive instruction, etc.

CodePudding user response:

refer to the second floor weixin_42054817 response:
thank you, a global variable is a method, or know how to pass parameters to the interface thread
global variable in c + + is not a good way, because 1, there are some inherent defects; 2, there are other good replacement methods at the same time, but the general company also have no more high quality requirements, so also can do,
  • Related