Waitforsingleobject Application
Are you still worried about how to judge whether a program is over? Maybe you want to use a loop every few seconds to determine whether the process is finished, but this will cause a problem. If the interval is too short, it will occupy CPU resources, the interval is too long to know that the program has ended in time. Maybe someone will immediately think of the waitforsingleobject function.
This function provided by the API can solve this problem, but it is easy to ignore some problems when using the waitforsingleobject function. The younger brother is not talented. Let's talk about it here.
Let's take a look at the original form of the function.
DWORDWaitforsingleobject(Handle hhandle, DWORD dwmilliseconds );
The hhandle parameter is the handle of the synchronization object.
The dwmilliseconds parameter is a timeout interval in milliseconds. If this parameter is set to 0, the function tests the state of the synchronization object and returns immediately. If this parameter is set to infinite, the timeout interval is infinite.
Table 12.1 return values of waitforsingleobject
The waitforsingleobject function enables the thread to block the waiting function of its own execution. It is returned only when the synchronization object as its parameter generates a signal. Or after the specified wait time is exceeded, the function will return regardless of the signal. When the waiting function does not return, the thread is in the waiting state. At this time, the thread only consumes a small amount of CPU time.
1. Wait for the created Process
How to Use the waitforsingleobject function? Suppose we start the Notepad program of the system and wait for the end of the notebook. The code can be written as follows:
Cstring strcommand = "";
Startupinfo Si = {sizeof (SI )};
Process_information PI;
Char cwindowsdir [max_path];
Char ccommand [max_path];
Getwindowsdirectory (cwindowsdir, max_path); // obtain the system directory
Strcommand = (cstring) cwindowsdir + "// notepad.exe ";
Strcpy (ccommand, strcommand); // obtain the Notepad program path.
Bool Bret = CreateProcess (null, ccommand, null, null, false, 0, null, null, & Si, & PI); // start the Notepad program
If (BRET)
{
If (! Waitforsingleobject (PI. hprocess, infinite) // wait for the Notepad program to close
{
Afxmessagebox ("Notepad is closed! ");
}
}
2. Wait for an existing process
Now that we know when notepad is closed, we can do what we want to do when it is closed. However, general programs are not created through CreateProcess. What should I do if I want to obtain programs in the running state? Yes. To get the process ID of the running program, we need to use the OpenProcess function to open an existing process object. The prototype is as follows:
HandleOpenProcess(DWORDDwdesiredaccess,
BoolBinherithandle,
DWORDDwprocessid
)
Dwdesiredaccess Parameter.
BinherithandleWhether the handle is inherited
Dwprocessid Process ID
Returned value: if the call is successful, the function returns a handle of the process. If the call fails Null .
How can I obtain the process ID? We need to use several other functions createconlhelp32snapshot, process32first, and process32next. These functions are in tlhelp32.h, used to enumerate system processes together (just as the task manager enumerates all processes)
Handle winapiCreateconlhelp32snapshot(
DWORDDwflags,
DWORDTh32processid
);
DwflagsThe parameter is a flag. Generally, th32cs_snapprocess is used,
Th32processidThe parameter is the process ID. If the first parameter is th32cs_snapprocess, this parameter is ignored.
Return value. If the call is successful, a snapshot handle is returned. If the call fails, invalid_handle_value is returned;
Bool winapiProcess32first(
Handle hsnapshot,
Lpprocessentry32 lppe
);
Hsnapshot ParametersYou need to assign a value to the handle using createconlhelp32snapshot.
Lppe ParametersIs the process entry information. You need to define a processentry32 type variable to obtain this value.
The parameters of process32next and process32first functions are the same. The three functions can enumerate the process continuously until the return value of process32next is false.
You can implement the process object waiting for the running process in this way.
Processentry32 pe32;
Handle hsnaphot;
Handle Happ;
DWORD dprocess;
Hsnaphot = createconlhelp32snapshot (th32cs_snapprocess, 0); // obtain the process Snapshot
Process32first (hsnaphot, & pe32); // point to the first process
Do
{
If (lstrcmpi (pe32.szexefile, _ T ("notepad.exe") = 0)// The process name is Notepad.exe (Notepad program)
{
Dprocess = pe32.th32processid;
Break;
}
} While (process32next (hsnaphot, & pe32 ));//Loop until the process is not available
Happ = OpenProcess (
Process_vm_operation | synchronize, false, dprocess );
// Based on Process ID Get the program handle
If (! Waitforsingleobject (HAPP, infinite ))//Wait for the Notepad program to close
{
Afxmessagebox ("Notepad is closed! ");
}
Now we know not only how to use the waitforsingleobject wait function, but also how to use functions such as getwindowsdirectory, CreateProcess, OpenProcess, createmedilhelp32snapshot, process32first, and process32next, these functions implement the functions we want together and play an important role in other aspects.