Home > Software engineering >  The child thread notify the main thread processing results
The child thread notify the main thread processing results

Time:10-19

[size=16 px] function is as follows:
Program has two threads: the main thread and the child thread,
Main thread sends a message to the child thread (postthreadmessage) do something else, after the child thread loop awaiting (while) the main thread of the message, and then to carry on the corresponding processing, after the child thread processing, then the processing result variables (such as a) sent to the main thread, the main thread receives the child thread sends to the message and then save the variable a, then continue to do your own thing,
My question is:
The main thread how to know when the child thread to send messages, cannot use circular wait, for the thread to do other things, [/size
In addition, do not use the window
New to this area, for the two days, hope everybody to help, thank you

CodePudding user response:

The child thread to thread cenfa news: : : PostMessage
You in to a custom message processing function.

CodePudding user response:

Another thread is a thread: : PostMessage knew there are threads coming

CodePudding user response:

The main thread to know when the child thread to send to the message? Cannot use circular wait, because the Lord has other thread to do [/size]

CodePudding user response:

Postmessage is put a message in the specified window to create the associated thread's message queue, but I didn't use Windows, the main thread is the main function

CodePudding user response:

reference 4 floor u012884346 response:
postmessage is put a message in the specified window to create the associated thread's message queue, but I didn't use Windows, the main thread is the main function

WIN32 program is your main thread, then put the two events are two child threads, synchronization by events, or send messages to complete between threads.

CodePudding user response:

reference 5 floor fengqinqdca reply:
Quote: refer to 4th floor u012884346 response:

Postmessage is put a message in the specified window to create the associated thread's message queue, but I didn't use Windows, the main thread is the main function

WIN32 program is your main thread, then put the two events are two child threads, synchronization by events, or send messages to complete between threads.

If only to establish a child thread, how can the child thread message (postmessage?) Threads to create it?

CodePudding user response:

refer to 6th floor u012884346 response:
Quote: refer to the fifth floor fengqinqdca reply:

Quote: refer to 4th floor u012884346 response:

Postmessage is put a message in the specified window to create the associated thread's message queue, but I didn't use Windows, the main thread is the main function

WIN32 program is your main thread, then put the two events are two child threads, synchronization by events, or send messages to complete between threads.

If only to establish a child thread, how can the child thread message (postmessage?) Threads to create it?


Your main thread (WIN32) no news cycle, how could postmessage, you open a single thread and operation between the main thread, this design has a problem,
Message can be postthreadmessage between threads

CodePudding user response:

I now is: in the main thread using the GetCurrentThreadId to obtain the main thread ID, then postthreadmessage specify the main thread in the child thread ID, you can send message to the main thread,
But how to make the main thread to send instruction to the child thread, and saved when a message from the child thread, just do something else, when no news???????

CodePudding user response:

Thank you colorful movement, I just contact with these, before the program only the main thread, the results of the need to wait for a task, don't want to wait now, when I received the result, you can handle it, there is no news to do other tasks, and how to design?

CodePudding user response:

 example code, the main thread WMCALC messages sent to the child thread, 

After the child thread processing, WMDISPLAY messages sent back to the

The main thread, the main thread to continue treatment,

I kiss,
on success,
The child thread processing their own message loop
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

# define WM_CALC WM_USER + 1

The child thread derived class

The class CUIThread: public CWinThread
{
DECLARE_DYNCREATE (CUIThread)

Public:
CUIThread ();//protected constructor, informs by dynamic creation
Virtual ~ CUIThread ();

Public:
Virtual BOOL InitInstance ();
Virtual int ExitInstance ();

CUIThreadDlg m_Dlg;

Protected:
DECLARE_MESSAGE_MAP ()
Afx_msg void OnCalcaulate (UINT wParam, LONG lParam);

};


//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Impletation

BOOL CUIThread: : InitInstance ()
{
//TODO: perform the and per - thread initialization here
Return TRUE;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

The main thread sends a message to another CWINTHREAD interface thread WMCALC

CUIThread * m_pCalculateThread=
(CUIThread *) AfxBeginThread (RUNTIME_CLASS (CUIThread)); Sleep (500);

M_pCalculateThread - & gt; PostThreadMessage (WM_CALC, 0, NULL);


After the thread the message response function

BEGIN_MESSAGE_MAP (CUIThread, CWinThread)
ON_THREAD_MESSAGE (WM_CALC CUIThread: : OnCalcaulate)
END_MESSAGE_MAP ()

Void CUIThread: : OnCalcaulate (UINT wParam, LONG lParam)
{
Sleep (500);
: : AfxMessageBox (L "THREAD_WM_CALC");
: : PostMessage ((HWND) (GetMainWnd () - & gt; GetSafeHwnd ()), WM_DISPLAY, 0, NULL);

}

Send the information to the main thread

ON_MESSAGE (WM_DISPLAY, & amp; CMFCMTTESTDlg: : OnDisplay)

LRESULT CMFCMTTESTDlg: : OnDisplay (UINT wParam, LONG lParam)
{
: : AfxMessageBox (L "MAIN_DLG_THREAD_WM_DISPLAY");
return 0;
}

CodePudding user response:



Click OK message back

CodePudding user response:

I haven't the window in my program, the main thread to know when it received the child thread to send the message? Can't cycle waiting, turn for help

CodePudding user response:

The original poster is the problem is too complex to

Try to no callback function?

Or add a thread

CodePudding user response:

What the final result?

CodePudding user response:

Alerts can be

CodePudding user response:

null
  • Related