In a program, these independent program fragments are called "Threads" threads.MultithreadingProcessing ". A common example of Multithreading is the user interface. By using the thread, you can press a button and the program will immediately respond, instead of waiting for the user to start responding after the program completes the current task.MFCMultithreading in is relatively simple. We recommend that you use AfxBeginThread to implement it. However, when using this, you do encounter the problem that the internal field data of the object cannot be obtained after this is passed in.
I. Problem code
1.1 Test. h
- #pragma once
- class CTest
- {
- public:
- CTest(void);
- ~CTest(void);
- void ThreadMethod(HWND hWnd);
- HWND m_hWnd;
- };
-
1.2 Test. cpp
- #include "StdAfx.h"
- #include "Test.h"
- CTest::CTest(void)
- {
- }
- CTest::~CTest(void)
- {
- }
- UINT ThreadProc(LPVOID lpParam)
- {
- CTest* test = (CTest*)lpParam;
- HWND hWnd = test->m_hWnd;
- return 0;
- }
- void CTest::ThreadMethod(HWND hWnd)
- {
- this->m_hWnd = hWnd;
- AfxBeginThread(ThreadProc,this);
- }
1.3 MFC main form method call
CTest test;
Test. ThreadMethod (m_hWnd );
1.4 debugging and instructions
Call the breakpoint to "HWND hWnd = test-> m_hWnd;" of the ThreadProc method. The execution result is null, and the value is assigned at CTest: ThreadMethod! In fact, it is also found that the variable type is int and can be passed. After the CString is passed, it is garbled or non-original data.
Ii. Solution
Declare test as a pointer and call the method using the pointer as follows:
Test = new CTest ();
Test-> ThreadMethod (m_hWnd );
Test can be declared in the header file, and the data is normal when the breakpoint is again.
The problem is relatively hidden, because it was previously called using pointers and then changed to an object call, so I guess the principle is not quite clear yet.