Analysis on parameter passing through multiple threads in MFC

Source: Internet
Author: User

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

 
 
  1. #pragma once  
  2. class CTest  
  3. {  
  4. public:  
  5. CTest(void);  
  6. ~CTest(void);  
  7. void ThreadMethod(HWND hWnd);  
  8. HWND m_hWnd;  
  9. };  
  10.  

1.2 Test. cpp

 
 
  1. #include "StdAfx.h"  
  2. #include "Test.h"  
  3. CTest::CTest(void)  
  4. {  
  5. }  
  6. CTest::~CTest(void)  
  7. {  
  8. }  
  9. UINT ThreadProc(LPVOID lpParam)  
  10. {  
  11. CTest* test = (CTest*)lpParam;  
  12. HWND hWnd = test->m_hWnd;  
  13. return 0;  
  14. }  
  15. void CTest::ThreadMethod(HWND hWnd)  
  16. {  
  17. this->m_hWnd = hWnd;  
  18. AfxBeginThread(ThreadProc,this);  
  19. }  

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.

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.