Afxbeginthread requires closehandle? Solution

Source: Internet
Author: User

Selected from: http://www.myexception.cn/vc-mfc/300534.html

1. afxbeginthread requires closehandle ???
Generally, closehandle is used to create threads.

But does afxbeginthread need it ???

Question 2:

When the thread is paused, if the main interface is closed at this time ....

How do I close the subthread ????

Question 3: For afxbeginthread threads, can the main thread exit the subthread in exitinstance mode ???

------ Solution --------------------------------------------------------
1. No. This is encapsulated by MFC.
2. When a thread is suspended, the user closesProgramThe correct method is, of course, to exit the suspended thread first,
The method first restores the thread and then communicates with the thread (it may be a global variable, event, or message, the first two
It is generally used to communicate with the Working thread. The latter is used to communicate with the UI thread or a thread with a self-writing message loop },
Let the thread exit by itself. The main thread waits for the thread handle (for example, wait for 1 second). If it cannot wait, call
Terminatethread forcibly terminates the thread. Of course, force termination may cause internal leakage,
So I 'd rather wait for a while, for example, two seconds.
3. What the rabbit Party organization Minister gaveCode, I think
Pthread-> m_bautodelete = false; // It is used to wait for the UI thread handle, otherwise the handle will be invalid after the thread returns. It seems unnecessary for working threads.
Pthread-> postthreadmessage (...); // here a wm_quit message is generally sent.
Waitforsingleobject (pthread-> m_hthread); // This is the thread handle. You should add another time, for example:
Waitforsingleobject (pthread-> m_hthread, 2000 );

Closehandle (pthread-> m_hthread); // is this sentence necessary?
Delete pthread; // This is to make the Thread class destructor
Pthread = NULL; // if you want to exit the program immediately, you can skip this sentence.

Other references: http://bbs.csdn.net/topics/70107258

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.