The first multi-threaded program + differences between objects and handles + insights

Source: Internet
Author: User

I first compiled a program, which is too slow. Therefore, I decided to use multiple threads to provide speed. I read some materials in the early stage and it was quite simple. So I tried it myself and used a very small case! After debugging the results for one day, I was depressed. I just want to summarize the process, which is a spur to myself!

The program compiled this time is very simple, it is a function that automatically obtains the system time. Here, a thread is used to continuously obtain the system time, which is displayed in the dialog box. Here, the thread function uses a static member function. The Code is as follows:

Void ctestdlg: onbnclickedok () {// todo: add your control notification handler code herehwnd hdlg; hdlg =: findwindow (null, "dialog box"); If (! Hdlg) afxmessagebox ("Sorry! "); Ctestdlg * pdlg = (ctestdlg *) cwnd: fromhandle (hdlg); afxbeginthread (afx_threadproc) ctestdlg: threadfun1, pdlg); getdlgitem (idok) -> enablewindow (false); getdlgitem (idcancel)-> enablewindow (true );}

This function enables the multi-threaded function to obtain the system time when you click OK. The multi-threaded functions are as follows:

DWORD WINAPI CTestDlg::ThreadFun1(LPVOID lpPara){ CTestDlg *pDlg=(CTestDlg*)lpPara;pDlg->m_bRun=TRUE;CTime time;CString strTime; while (pDlg->m_bRun) { time=CTime::GetCurrentTime(); strTime=time.Format("%H:%M:%S"); pDlg->SetDlgItemText(IDC_TIME,strTime); pDlg->UpdateWindow(); Sleep(1000); } return 0;}

A cancel function is added later to complete the program:

Void ctestdlg: onbnclickedok () {// todo: add your control notification handler code herehwnd hdlg; hdlg =: findwindow (null, "dialog box"); If (! Hdlg) afxmessagebox ("Sorry! "); Ctestdlg * pdlg = (ctestdlg *) cwnd: fromhandle (hdlg); afxbeginthread (afx_threadproc) ctestdlg: threadfun1, pdlg); getdlgitem (idok) -> enablewindow (false); getdlgitem (idcancel)-> enablewindow (true );}

This is your own function. So where is the problem?

1. Problems Occurred When passing parameters to threadfun1;

Second, a problem occurs when setdlgtext is called in threadfun1.

First. I first defined a ctestdlg object, and passed the pointer of this object to threadfun1 ,. In fact, it is obviously incorrect here. Because the domodal call dialog box was called in the menu bar at that time, I was not familiar with how to obtain the domodal function call dialog box. So I took it for granted that I would define another ctestdlg object and use parameters. In fact, this analysis is very obvious. Because there is no logical connection between the two, how can this problem be solved?

I also tried to pass the window handle as a parameter to the thread function, but I didn't understand it here.Window handle and window object. So I checked it online and the results are as follows:

From the syntax structure:

The window handle is a concept in windows. Because it is written in C, windows are written in struct of C (not the struct of CPP ). It is just a description of some window properties. How can I identify this window? The pointer can be identified, but Ms encapsulates the pointer into a handle to hide the implementation details.
The concept of cwnd in MFC is a C ++ class. A cwnd object corresponds to a window. cwnd encapsulates a member called m_hwnd, this is the corresponding window handle. This variable is equivalent to a series of member variables that describe window properties. In addition, it also encapsulates some operations on this window handle.
Windows SDK uses API functions to control hwnd, but these operations are global functions. Without CPP, these operations are encapsulated into classes. MFC encapsulates the operations of these Apis into the cwnd class.

From the logic relationship:
Objects in MFC, such as application objects and view objects, occupy the memory space. We can use pointers to this object for access, but windows also provides handle access, it seems a little redundant at first glance, but not actually. If one of our processes wants to access another process, access based on the object pointer will not work. Windows is a fully protected system. Applications work in the CPU protection mode and the virtual memory technology is introduced. Each process has an independent 4 GB address space, so the address in the application is the address in your own eyes, not universal.
The two processes cannot simply pass an address. The form handle is a data structure of the Windows Kernel. Different Forms (can be different forms of different processes) there are different form handles. Windows can identify different form objects (corresponding to the previous global function) through the handle ).

Summarize the following:
Class objects are class instantiation, and class pointers are also equivalent to class instantiation. However, because of the closeness (privacy protection) of classes, the exchange between classes is not allowed. In Windows, A Global handle is defined to implement class-to-class exchange. This handle is encapsulated and is equivalent to an object pointer. HoweverIts function is completely different from that of the object pointer. Object pointers are instantiation of classes and are defined to operate or process objects. The handle is defined for interaction between classes. Therefore, it does not have the function to operate on specific objects and must be converted to class objects or object pointers.In short, the differences between the two of them are determined from the design point.

The second is that when you call setdlgtext, the window handle is used, which cannot be displayed at this time. I searched on the Internet and also checked msdn. class objects should be used here. In addition, based on the above understanding of objects and handles, this understanding is more profound.

Finally, the most important thing is to summarize your problems.. Because it takes half of the time to complete the subject and half of the time to complete the other tasks. After several practices, I finally found my faults. When conquering a subject, you will find a lot of information and straighten out everything in a down-to-earth Manner, so it is relatively fast. However, there is no error in solving the problem that the "xiafan crab" outside the subject is always the first goal. But at this time, I thought of solving the problem without analyzing the problem. As a result, it has always been so frustrating.

I watched the Cold War last night. The movie impressed me the most: I had to stay calm when I encountered a problem at a critical moment. I stopped to analyze the problem carefully, do not be irritated by the appearance of the problem, but enter the control area of others. When the robbers demanded a ransom from more than 90 million to more than 30 million, they had to spend half an hour reorganizing it. In fact, this is a reaction under high pressure, and the clever Liu sir is also confused by the surface of the problem. Under normal circumstances, this story can be completed immediately, so there is no need for such troublesome things. Finally, Liu sir's Jedi counterattack is just luck. If the robbers are not in the police station, isn't everything too late?

In fact, this is the same as the above mentioned. When a problem occurs, it is the key point. At this time, we should be more calm and carefully analyze the causes for the problem.Fundamentally cut off the root cause of the problem, rather than being dominated by the problem!

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.