MFC dialog box in Lotice Solution Works

zhaozj2021-02-11  199

Question: I use CreateThread to generate a thread directly, then try to call CDialog derived classes directly in the thread, the result is possible, but when there is a message such as lbuttondown or mouse to move to the title bar, debug in, debug in, discover the following statement :

(WinCore.cpp)

// Note: if Either of the Above asserts fire and you are

// Writing a multithreaded Application, IT is Likey That

// you have passed a c Object from One Thread to Another

// and have used That Object in A Way That Was Not Intended.

// (Only Simple Inline Wrapper Functions Should Be Used)

//

// in General, CWND Objects Should Be Passed by hwnd from

// One thread to another. The receiving thread Can Wrap

// The hwnd with a cWnd Object by using cWnd :: fromHandle.

//

// IT Is Dangerous to Pass C Objects from One Thread To

// another, unless to be used in

// Such a manner.

In fact, I didn't pass any variables from the primary thread to the child thread. The problem may be in the way I did not use the derived thread class to use the thread, using the MFC's derived class in the global thread function, causing the new window without default parents. Window ----- causes the message that the message cannot be properly transferred and passed, ie the dialog window does not have a normal reception message, which cannot process the message, which causes a fatal error. So when you create a thread, I will pass the window handle as a parameter to the child thread, as shown below:

:: CreateThread (Null, 0, ListenMessage, this-> m_hwnd, 0, & clientthreadid);

Accept the handle within the subline and pass it to cdialog derived class:

HWnd hwnd = (hwnd) lparg;

CWND * WND = CWND :: fromHandle (HWND);

CXXXXDLG MYDLG (WND);

Then compile (PNT 4.0 VC6), running well.

Generally speaking, all MFCs, do not have this situation in the use of threads, I am generally avoided because I am not fully prepared, causing this embarrassing situation, usually should be avoided, when using this way, when you create a thread, you should try to Avoid interface threads, simple working thread work is equally highly efficient. In addition, in order to guarantee problems, the properties of the dialog are Pop and None Border, no system menu.

Important statement, this method has a guess, I don't commit commitment, if you are unfortunately, I am afraid to ride me, I hope that the method will help you.

If you have help you, please send me an email (cd_yuan@sina.com), if you want to swear, please send a letter to the moderator.

转载请注明原文地址:https://www.9cbs.com/read-3931.html

New Post(0)