An error occurred while using afxgetmainwnd () in the thread. Afxgetmainwnd () returns the main window of the current thread (if any ).
Because the main window belongs to the main thread, So you want to get the master Window Hwnd Value ,
Only in the main thread Use afxgetmainwnd () , But if it wasn't in the main thread,
Afxgetmainwnd () may be used to query the main window from the current thread. But it seems
Afxgetmainwnd () cannot cross-thread, so an error occurs. To use
The hwnd value of the main window. You can pass the hwnd value of the main window to the thread.
You can use afxgetapp () to obtain the main thread first, and then use the cwinthread class
Member m_pmainwnd obtains the main window (afxgetapp ()-> m_pmainwnd-> m_hwnd)
If afxgetmainwnd is called from the application's primary thread,
It returns the application 'smain window according to the above rules.
If the function is called from a secondary thread in the application,
The function returns the main window associated with the thread that made the call.
I found the reason today ~ My thread is usedCreatethread is created
The interface provided by the operating system, inCreate createthreadUsing MFC functions in the threadAfxgetmainwnd ()There will be problems.
From: BBS shuimu Tsinghua station: essence District
if you use MFC for programming, do not use createthread. If you only use the Runtime Library, use
_ begingthread. In short, do not use createthread easily
This is because functions in MFC and RTL may use the public variables they encapsulate, that is,
both afxbeginthread and _ beginthread have their own startup. the Code is not included in createthread.
createthread is used when the MFC class and RTL function are used in the created thread, problems may occur.
If you compile a Win32 program in assembly, and in the thread function, the MFC and RTL functions are not called, if you use
createthread, there is no problem, or you use C to write the thread function, but you are careful not to call the RTL function
and there will be no problem
createthread is an interface provided by the operating system, while afxbeginthread and _ beginthread are the compilation
tool's encapsulation of them