event天生的弱势,只有mutex可以感知丢失,就是将另一个进程关闭了,event无法感知。
event1:
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[100] = "haihualovefang"; void main() { HANDLE event = CreateEventA(NULL, FALSE, FALSE, name); printf("创建成功"); char ch = getch(); SetEvent(event); printf("触发event"); CloseHandle(event); system("pause"); }
event2:
#include <stdio.h> #include <stdlib.h> #include <Windows.h> char name[100] = "haihualovefang"; void main() { HANDLE event = OpenEventA(EVENT_ALL_ACCESS, TRUE, name);//打开事件 if (event == NULL) { printf("打开失败"); system("pause"); return; } printf("等待-------"); DWORD res = WaitForSingleObject(event, 20000); switch (res) { case WAIT_OBJECT_0: printf("收到信号-------"); break; case WAIT_TIMEOUT: printf("超时没有收到-------"); break; case WAIT_ABANDONED: printf("另外一个进程意外终止-------"); break; default: break; } CloseHandle(event); system("pause"); }
时间: 2024-10-02 05:34:19