Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of multithreading parameter transfer in MFC

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to solve the problem of MFC multithreading parameter transfer". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to solve the problem of MFC multithreading parameters.

In a program, these independently running program fragments are called "Thread", and the concept of programming with it is called "multithreading". A common example of multithreading is the user interface. With threads, the user can press a button and the program responds immediately, rather than waiting for the program to complete the current task before starting to respond. It is relatively simple to use multithreading in MFC, and it is recommended to use AfxBeginThread, but you do encounter the problem that you cannot get the internal field data of the object after you input this.

I. problem code

1.1 Test.h

# pragma once class CTest {public: CTest (void); ~ CTest (void); void ThreadMethod (HWND hWnd); HWND masked 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- > massihWnd; 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 description

Type the breakpoint to "HWND hWnd = test- > masked hWnd;" of the ThreadProc method, and the execution discovers that it is empty, but it is clearly assigned at CTest::ThreadMethod! In practice, it is also found that the variable type is int can be passed, after CString transmission is garbled or non-raw data.

II. Solutions

Declare the test as a pointer and call the method through the pointer, as follows:

Test = new CTest ()

Test- > ThreadMethod (m_hWnd)

Test can be declared in the header file, and the breakpoint again finds that the data is normal.

At this point, I believe that everyone on the "MFC multithreaded parameter transfer problem how to solve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report