Use of the waitforsingleobject Function

Source: Internet
Author: User

Waitforsingleobject
This function returns when the specified object is in the signal or wait for the end of time.
DWORD waitforsingleobject (
Handle hhandle,
DWORD dwmilliseconds
);
Parameters:
Hhandle: Specifies the handle of an object or event;
Dwmilliseconds: wait time, in the unit of no effect. When the wait time is exceeded, this function returns. If this parameter is set to 0, the function will return immediately. If this parameter is set to infinite, the function will not return until there is a signal.
Return Value:
If the function succeeds, the returned result of the function identifies the event that is returned by the function. The return value is as follows:
Wait_abandoned (0x00000080l)
The specified object is a mutex, and the object is not released by the thread that owns the object before the end of the thread. The ownership of the mutex object is granted to the thread that calls the function. The mutex object is set to a non-signal state.
Wait_object_0 (0x00000000l)
The specified object has a signal.
Wait_timeout (0x00000102l)
The waiting time is exceeded, and the specified object is in the stateless state.
If it fails, wait_failed is returned;
Note:
This function checks the status of the specified object or event. If the object is in the non-signal state, the calling thread is in the waiting state. At this time, the thread does not consume CPU time,
This function can wait for the following objects:
Change Notification
Console input
Event
Job
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable Timer
Requirements:
Client requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows ME, Windows 98, or Windows 95.
Server requires Windows Server 2003, Windows 2000 server, or Windows NT Server.
Header declared in WINBASE. h; Include windows. h.
Library link to kernel32.lib.
DLL requires kernel32.dll.

Program example:
1. Create a dialog box application named mytestthread
2. Add a button named Start and Stop. In the dialog box, add an edit box with the ID of idc_time,
3. Add the member variable handle m_hthread [2]. This is the thread handle;
4. Define global variables to control the running of threads;
Volatile bool m_threadrun [2];
5. Add a global event object to monitor the thread and control whether the thread is running.
Cevent event;
Note: objects defined in 4 and 5 must be defined in the. cpp file;
6. Declare the callback function. The callback function must be a global or static function. The statement is as follows:
Void threadfunc1 (lpvoid pparam );
Void threadfunc2 (lpvoid pparam );
The implementation of the callback function is as follows:
Void threadfunc1 (lpvoid pparam)
{
Ctime time;
Cstring strtime;
Event. resetevent ();
M_threadrun [0] = true;
M_threadrun [1] = true;
DWORD threadid =: getcurrentthreadid ();
While (m_threadrun [0])
{
Time = ctime: getcurrenttime ();
Strtime = time. Format ("% H: % m: % s ");
Cmytestthreaddlg * pdlg = (cmytestthreaddlg *) pparam;
Pdlg-> setdlgitemtext (idc_time, strtime );
Sleep (1000 );
}
}

Void threadfunc2 (lpvoid pparam)
{

Ctime time;
Cstring strtime;
DWORD threadid =: getcurrentthreadid ();

// If the event is in the signal state, the thread starts to run after the following function is executed. If the event is in the signal state, the following function is executed.

// Then, the thread is in the waiting state until there is a signal to start running;
: Waitforsingleobject (event, infinite );
While (m_threadrun [1])
{
Time = ctime: getcurrenttime ();
Strtime = time. Format ("% H: % m: % s ");
Cmytestthreaddlg * pdlg = (cmytestthreaddlg *) pparam;
Pdlg-> setdlgitemtext (idc_time, "OK ");
Sleep (1000 );
: Waitforsingleobject (event, infinite );
}
}

7. Define the member variable for saving the thread ID: DWORD m_threadid [2];
8. Add a message response function for the start and stop buttons as follows:
Void cmytestthreaddlg: onbnclickedok ()
{
// Todo: add the control notification handler code here
M_hthread [0] =: createthread (null, 0, (lpthread_start_routine) threadfunc1, this, 0, & m_threadid [0]);
M_hthread [1] =: createthread (null, 0, (lpthread_start_routine) threadfunc2, this, 0, & m_threadid [1]);

Getdlgitem (idc_button1)-> enablewindow (false );
Getdlgitem (idc_button2)-> enablewindow (true );
}

Void cmytestthreaddlg: onbnclickedcancel ()
{
M_threadrun [0] = false;
Event. setevent ();
Getdlgitem (idc_button1)-> enablewindow (true );
Getdlgitem (idc_button2)-> enablewindow (false );
}

Compile and run the program and set the breakpoint to view the running status.

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.