Usage of 1.WaitForSingleObject

Source: Internet
Author: User

Usage of 1.WaitForSingleObject

				
DWORD
WaitForSingleObject (
HANDLE Hhandle,
DWORD dwmilliseconds
  
                     );
  

The parameter hhandle is a handle to an event, and the second parameter, Dwmilliseconds, is the time interval. If the time is signaled the state returns WAIT_OBJECT_0, if the time exceeds the dwmilliseconds value but the time event or no signal state returns WAIT_TIMEOUT.

Hhandle can be a handle to the following objects:
Change notification
Console input
Event
Job
Memory Resource Notification
Mutex
Process
Semaphore
Thread
waitable timer

The WaitForSingleObject function is used to detect the signal state of the Hhandle event, and returns when the function executes more than dwmilliseconds, but if the parameter dwmilliseconds is INFINITE The time function will not return until the corresponding time event becomes signaled, otherwise it will wait until WaitForSingleObject has returned straight to execute the following code. Here's an example:

First create a global Event object g_event:

CEvent g_event;

You can set the event to a signaled state by calling Cevent::setevent in the program.

Here is a thread function Mythreadpro ()

UINT Cflushdlg::mythreadproc (LPVOID pparam)
{

WaitForSingleObject (G_event,infinite);
for (;;)
{
............ .

}
return 0;
}

The following for loop is executed only when the g_event is set to signaled state in this thread function, because g_event is a global variable, so we can pass the g_event in other threads. SetEvent controls this thread.

Another usage is that we can use the WaitForSingleObject function to interval the function body of executing a thread function

UINT Cflushdlg::mythreadproc (LPVOID pparam)
{
while (WaitForSingleObject (g_event,mt_interval)!=wait_object_0)
{
..................
}
return 0;
}

In this thread function, you can set Mt_interval to control how often the function body of this thread executes once, and when the event is not signaled, the function body Mt_interval executes once, and when the event is signaled, the thread executes.

2. WaitForSingleObject
This function returns when the specified object is in a signaled state or a state where the wait time has ended.
DWORD WaitForSingleObject (
HANDLE Hhandle,
DWORD dwmilliseconds
);
Parameters:
Hhandle: A handle to the specified object or event;
Dwmilliseconds: The wait time, in milli-units, is returned when the wait time is exceeded. If the parameter is set to 0, the function returns immediately, and if set to infinite, the function is not returned until it has a signal.
return value:
If this function succeeds, the function's return identifies the event that caused the function to return. The return values are as follows:
Wait_abandoned (0x00000080l)
The specified object is a mutex object that is not freed before the thread that owns the object is finished. Ownership of the mutex object is granted to the thread that called the function. The mutex object is set to a signal-free state.
WAIT_OBJECT_0 (0x00000000l)
The specified object has a signaled state.
Wait_timeout (0x00000102l)
The specified object is in a no-signal state over the wait time
If it fails, return to wait_failed;
Note:
This function checks the state of the specified object or event, and if the object is in a signal-free state, the calling thread is in a wait state, and the thread does not consume CPU time.
The function can wait for the following objects:
Change notification
Console input
Event
Job
Memory Resource Notification
Mutex
Process
Semaphore
Thread
waitable timer
Demand:
Client Requires windows XP, Windows Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
Server Requires Windows Server 2003, Windows $ server, or Windows NT server.
Header declared in Winbase.h; Include Windows.h.
Library Link to Kernel32.lib.
DLL Requires Kernel32.dll.

Examples of programs:
1. Create a dialog application with the project name Mytestthread
2, add the button, named Start and stop, add the edit box in the dialog box, ID is idc_time,
3, add member variable, HANDLE m_hthread[2], this is the handle of the thread;
4, the definition of global variables, to control the operation of the thread or not;
Volatile BOOL m_threadrun[2];
5. Increase the global event object to monitor the thread and control whether the thread is running.
CEvent event;
Note: Objects defined in 4, 5, must be defined in the. cpp file;
6, declare the callback function. The callback function must be a global function or a static function. The declaration is as follows:
void ThreadFunc1 (LPVOID pparam);
void ThreadFunc2 (LPVOID pparam);
The callback function is implemented 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 ();

Event is signaled, the following function executes, the thread starts running, and if event is not signaled, the function below executes

, the thread waits until a signal starts to run;
:: 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 that holds the thread ID: DWORD m_threadid[2];
8. Add the message response function to the start and stop buttons as follows:
void Cmytestthreaddlg::onbnclickedok ()
{
TODO: Add 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 run, set breakpoints, and see how it works.

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.