One-day training of Windows APIs (43) waitforsingleobject Function

Source: Internet
Author: User

The preceding section describes how to create a simple thread where the waitforsingleobject function is used. What is it used? In fact, it is used to implement the process of waiting for the thread to end. Its Mechanism is as follows: to notify the Windows operating system, now I am in sleep state. When the object conditions I am concerned about are met, if yes, wake me up. The object concerned here is whether the thread exits. This is a condition test. If you delete the resources of a thread without waiting for the thread to be closed, an error will occur. Using the preceding thread is simpler, but it is not suitable for reuse. Now we use the C ++ class to encapsulate a better class, in this way, you can easily create multiple threads for use. Of course, you can inherit from it to implement more complex functions. Here is an example.

The waitforsingleobject function declaration is as follows:

Winbaseapi
DWORD
Winapi
Waitforsingleobject (
_ In handle hhandle,
_ In DWORD dwmilliseconds
);
Hhandle is the handle of the waiting object.
Dwmilliseconds is the time condition for waiting, and can wait forever.

An example of calling this function is as follows:

#001 # pragma once
#002
#003 // Thread class.
#004 // Cai junsheng 2007/09/23
#005 class cthread
#006 {
#007 public:
#008
#009 cthread (void)
#010 {
#011 m_hthread = NULL;
#012}
#013
#014 virtual ~ Cthread (void)
#015 {
#016 if (m_hthread)
#017 {
#018 // Delete the thread resource.
#019: closehandle (m_hthread );
#020}
#021
#022}
#023
#024 // create a thread
#025 handle createthread (void)
#026 {
#027 // create a thread.
#028 m_hthread =: createthread (
#029 null, // use the default security attribute.
#030 0, // The stack size of the thread.
#031 threadproc, // address of the thread running function.
#032 this, // The parameter passed to the thread function.
#033 0, // create a flag.
#034 & m_dwthreadid); // ID of the created thread.
#035
#036 return m_hthread;
#037}
#038
#039 // wait for the thread to end.
#040 void waitfor (DWORD dwmilliseconds = infinite)
#041 {
#042 // wait for the thread to end.
#043: waitforsingleobject (m_hthread, dwmilliseconds );
#044}
#045
#046 protected:
#047 //
#048 // run the function in a thread.
#049 // Cai junsheng 2007/09/21
#050 //
#051 static DWORD winapi threadproc (lpvoid lpparameter)
#052 {
#053 // output to the debugging window.
#054: outputdebugstring (_ T ("threadproc thread function run/R/N "));
#055
#056 // The thread return code.
#057 return 0;
#058}
#059
#060 protected:
#061 handle m_hthread; // thread handle.
#062 DWORD m_dwthreadid; // thread ID.
#063
#064 };
#065

Use this thread:
#001 //
#002 // response command.
#003 // Cai junsheng 2007/09/21 QQ: 9073204
#004 //
#005 lresult ccaiwinmsg: oncommand (int nid, int nevent)
#006 {
#007 // menu option command response:
#008 switch (NID)
#009 {
#010 case idc_createbtn:
#011 // display a button.
#012 if (! M_hbtn)
#013 {
#014 m_hbtn = createwindow (_ T ("button"), _ T ("button "),
#015 ws_visible | ws_child | bs_pushbutton,
#016 50,50, 100,32,
#017 m_hwnd, (hmenu) idc_btn, m_hinstance, null );
#018}
#019 break;
#020 case idc_btn:
#021 {
#022 cthread threaddemo;
#023 threaddemo. createthread ();
#024 threaddemo. waitfor ();
#025
#026 //
#027 outputdebugstring (_ T ("click/R/N "));
#028}
#029 break;
#030 default:
#031 return ccaiwin: oncommand (NID, nevent );
#032}
#033
#034 return 1;
#035}

Related Article

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.