Windows API createevent function usage

Source: Internet
Author: User

CreateEvent is the creation of Windows event objects, which are primarily used to determine thread exits and locks.

HANDLE CreateEvent (

lpsecurity_attributes lpeventattributes , //SD

BOOL bmanualreset, //reset type

BOOL binitialstate, //initial state

LPCTSTR lpname //object name);


Parameter description:

Lpeventattributes:

[Enter] A pointer to the SECURITY_ATTRIBUTES structure to determine whether the returned handle can be inherited by the quilt process. If lpeventattributes is null, this handle cannot be inherited.

Members in the structure of Windows nt/2000:lpeventattributes Specify a security character for the new event. If lpeventattributes is null, the event obtains a default security character.

bManualReset:

[input] Specifies whether the event object is created to be manually restored or automatically restored. If true, you must use the ResetEvent function to manually restore the state of the event to a signal-free state. If set to False, when an event is released by a waiting thread, the system automatically reverts the event state to a no-signal state.

Binitialstate:

[input] Specifies the initial state of the event object. If true, the initial state is signaled, otherwise there is no signal state.

Lpname:

[Enter] The name of the object that specifies the event, which is a string pointer that ends with 0. The character format of the name is limited to MAX_PATH. The name is case-sensitive.

If the name specified by Lpname is the same as the name of an existing named event object, the function requests event_all_access to access the existing object. At this point, because the bManualReset and binitialstate parameters are already set in the process of creating the event, these two parameters are ignored. If Lpeventattributes is a parameter that is not NULL, it determines whether the handle can be inherited, but its security descriptor members are ignored.

If lpname is null, an unnamed event object is created.

If the lpname is the same as an existing signal, mutex, wait timer, job, or file-mapped object name, the function will fail and the Error_invalid_handle will be returned in the GetLastError function. This behavior is caused by the fact that these objects share a single namespace.


Take the train ticket booking system for example:

CreateEvent.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <windows.h> #include <iostream> #include <string>using namespace Std;d Word WINAPI fun1proc (lpvoid lpparameter);D word WINAPI fun2proc (LPVOID lpparameter); int tickets=100; HANDLE g_hevent;void Main () {HANDLE hthread1,hthread2;   HANDLE g_hevent1;g_hevent1=createevent (null,false,false,_t ("tickets")); Let the application have only one instance if (g_hevent1) {if (Error_already_exists==getlasterror ()) {cout<< "Only single instance can run!" <<endl;return;}}  G_hevent=createevent (Null,false,true,null); Create an automatic reset event kernel object, non-signaled state hthread1=createthread (null,0,fun1proc,null,0,null); Hthread2=createthread (Null,0,fun2proc, Null,0,null);               CloseHandle (HTHREAD1); Closing the handle does not terminate the thread, indicating that the main thread is not interested in referring to the new threads, the reference count is reduced by 1CloseHandle (hThread2);//setevent (g_hevent); Sleep (4000); CloseHandle (g_hevent); System ("Pause");} DWORD WINAPI Fun1proc (LPVOID lpparameter)//When the reference count is 0 o'clock, the terminating thread {while (TRUE) {WaitForSingleObject (g_hevent,infinite);// ResetEvent (g_hevent); if (tickets>0) {SlEEP (1);cout<< "Thread1 sell tickes:" &LT;&LT;TICKETS--&LT;&LT;ENDL;} Else{break;} ResetEvent (g_hevent); SetEvent (g_hevent);} return 0;} DWORD WINAPI Fun2proc (LPVOID lpparameter) {while (TRUE) {WaitForSingleObject (g_hevent,infinite);//resetevent (g_ hevent); if (tickets>0) {cout<< "Thread2 sell tickes:" &LT;&LT;TICKETS--&LT;&LT;ENDL;} Else{break;} ResetEvent (g_hevent); SetEvent (g_hevent);} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows API createevent function usage

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.