- Create an event with an ID
1 Char name["myevent"; 2 Event = Createeventa (NULL, False, False, name);
- Setting events
1 SetEvent (event);
- Close Event
1 CloseHandle (event);
- Another process Open event
1 Event = Openeventa (event_all_access, TRUE, name); // Open Event
- And then wait for the event
1 DWORD res = WaitForSingleObject (event20000);
- Waiting for results
1 CaseWAIT_OBJECT_0:2printf"Signal received-------");3 Break;4 Casewait_timeout:5printf"Timeout not received-------");6 Break;7 Casewait_abandoned:8printf"another process terminated unexpectedly-------");9 Break;
Full code:
Event.c
1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 Charname[ -] ="MyEvent";6 //mutex can perceive loss, event cannot perceive7 voidMain ()8 {9HANDLEEvent=Createeventa (NULL, False, False, name);Tenprintf"created successfully"); One CharCH =getch (); A -SetEvent (Event); -printf"Triggering event"); theCloseHandle (Event); - -System"Pause"); -}
Wait.c
1#include <stdio.h>2#include <stdlib.h>3#include <Windows.h>4 5 Charname[ -] ="MyEvent";6 7 voidMain ()8 {9HANDLEEvent= Openeventa (event_all_access, TRUE, name);//Open EventTen One if(Event==NULL) A { -printf"Open Failed"); -System"Pause"); the return; - } -printf"Waiting for-------"); -DWORD res = WaitForSingleObject (Event,20000); + Switch(RES) - { + CaseWAIT_OBJECT_0: Aprintf"Signal received-------"); at Break; - Casewait_timeout: -printf"Timeout not received-------"); - Break; - Casewait_abandoned: -printf"another process terminated unexpectedly-------"); in Break; - default: to Break; + - } theCloseHandle (Event); * $ Panax Notoginseng - theSystem"Pause"); +}
26.event Cross-process communication