An error occurred while using afxgetmainwnd () in the thread.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.