Home > Software engineering >  AfxBeginThread create interface thread, the thread of the data is passed to the main process is what
AfxBeginThread create interface thread, the thread of the data is passed to the main process is what

Time:01-07

Main process create threads using AfxBeginThread CMyThread, created in CMyThread CMyDlg
CMyDlg in response to it in the window button, need to put some data is passed to the main process?
What good method is there?

CodePudding user response:

Everyone a great god door, I know this kind of situation with worker thread, better still, but I wanted to know with interface, thread interface thread in the window of how data out to the main process
Thank you for the

CodePudding user response:

Seems only SendMessagee

CodePudding user response:

Impression of MFC project multiple threads to their open window ways, tread pit,,
So now I don't try this way, can interface is the interface, function other separate,
Communication between threads, news, mailslot, and pipe, filemapping, etc, if you are not sure how to expand into later, will use the socket,

CodePudding user response:

The parameters in the thread function LPVOID universal pointer, can construct a structure, the need to pass information window, the window handle of the thread according to transfer information

For example,
 
//thread parameter structure
Typedef struct _tagThreadParam_t
{
HANDLE hThread;//thread handle
BOOL bThreadExitFlag;//thread to exit the tag
The HWND HWND;//window handle
Int iCount;
//...

_tagThreadParam_t ()
{
Memset (this, 0, sizeof (* this));
}
} THREADPARAM, * LPTHREADPARAM;
//thread parameters (note that its life cycle)
THREADPARAM tParam;

//thread function
DWORD CALLBACK TestThread (LPVOID lParam)
{
LParam THREADPARAM * pThreadParam=(THREADPARAM *);

while(! PThreadParam - & gt; BThreadExitFlag)/* the exit sign */
{
//update window control
Cstrings szVal;
SzVal. The Format (_T (" % d "), pThreadParam - & gt; ICount++);
SetDlgItemText (pThreadParam - & gt; HWnd IDC_TEST_EDIT, szVal);

//thread
Sleep (100);
}

return 0;
}

//start the thread
Void CdlgTDlg: : OnBnClickedButton1 ()
{
TParam. BThreadExitFlag=0;
TParam. HWnd=m_hWnd;
TParam. ICount=1000;
TParam. HThread=CreateThread (NULL, 0, TestThread, & amp; TParam, 0, NULL);
}

//stop thread
Void CdlgTDlg: : OnBnClickedButton2 ()
{
TParam. BThreadExitFlag=1;
If (tParam. HThread)
{
WaitForSingleObject (tParam hThread, 1000);
The CloseHandle (tParam. HThread);
TParam. HThread=NULL;
}
}



  • Related