Not available in another thread ((CMainFrame *) AfxGetMainWnd ())

Source: Internet
Author: User

A VC6 project was put under the VC8 to recompile here, anyway, it's hard to find some information. No fruit later turned to a foreigner's answer

If AfxGetMainWnd is called from the application ' s primary thread, it returns the application ' s main window according to th E 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's made the call.

This means that calling AfxGetMainWnd () in a child thread returns the form handle associated with the current line threads instead of the main form handle of the current program. I do not know whether this is a change of VC8, there is no time to investigate.

Workaround 1:
cwnd* M_pcwnd = NULL;
In OnInitDialog M_pcwnd = AfxGetMainWnd ();

Workaround 2:
Call AfxGetMainWnd () to replace the place with AfxGetApp ()->m_pmainwnd

Run problem resolution after compiling

I use method 2 to solve the problem.

AfxGetMainWnd () Gets the main window of the current thread (if any).
Because the main window belongs to the main thread, I want the main window HWND value,
You can only use AfxGetMainWnd () in the main thread, but not in the main thread.
AfxGetMainWnd () may be querying the main window from the current thread. But it seems
AfxGetMainWnd () cannot cross threads, so an error occurs. To use in the thread
The HWND value of the main window, which can pass the HWND value of the main window to the thread.
You can use AfxGetApp () first to obtain the main thread, and then through the CWinThread class into
m_pMainWnd Get 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.

Today seems to find a reason ~ My thread was created with CreateThread, and CreateThread is
The interface provided by the operating system, with MFC's function AfxGetMainWnd () will be problematic in the thread created by CreateThread.

If you use MFC programming, do not use CreateThread, if you just use the runtime Library, with
_beginthread, in short, do not use CreateThread easily
This is because functions in MFC and RTL may use some of the common variables that they encapsulate, that is,
Says AfxBeginThread and _beginthread have their own boot code that CreateThread didn't have.
It is possible to have problems using MFC's classes and RTL functions in threads created with CreateThread
If you are writing Win32 programs in assemblies and do not invoke MFC and RTL functions in the thread function, use the
CreateThread is no problem, or you are using C write thread function, but you are careful not to call the RTL function
It won't be a problem.

CreateThread are interfaces provided by the operating system, while AfxBeginThread and _beginthread are compiled
The package of the device to it

Not available in another thread ((CMainFrame *) AfxGetMainWnd ())

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.