本文共 1842 字,大约阅读时间需要 6 分钟。
//事件置为0无信号
BOOL ResetEvent(
HANDLE hEvent //事件句柄 );PulseEvent
// Event.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "conio.h"#include "windows.h"HANDLE g_hEvent = NULL;HANDLE g_hEvent2= NULL;DWORD WINAPI ThreadSend( LPVOID pParam ){ while( 1 ) { //触发事件(置为有信号) SetEvent( g_hEvent ); Sleep( 500 ); SetEvent( g_hEvent2 ); Sleep( 500 ); } return 0;}DWORD WINAPI ThreadRecv( LPVOID pParam ){ while( 1 ) { //等候事件通知,此事件等待到之后自动置为无信号 WaitForSingleObject( g_hEvent, INFINITE ); printf( "Hello Event: %p\n", g_hEvent ); } return 0;}DWORD WINAPI ThreadRecv2( LPVOID pParam ){ while( 1 ) { //等候事件通知 WaitForSingleObject( g_hEvent2, INFINITE ); printf( "Hello Event2: %p\n", g_hEvent2 ); ResetEvent( g_hEvent2 );//置为无信号 } return 0;}void Create( ){ DWORD nThreadID = 0; HANDLE hThread[3] = { NULL }; hThread[0] = CreateThread( NULL, 0, ThreadSend, NULL, 0, &nThreadID ); hThread[1] = CreateThread( NULL, 0, ThreadRecv, NULL, 0, &nThreadID ); hThread[2] = CreateThread( NULL, 0, ThreadRecv2, NULL, 0, &nThreadID );}int main(int argc, char* argv[]){ //创建自动重置事件 g_hEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); //创建手动重置事件 g_hEvent2 = CreateEvent( NULL, TRUE, FALSE, NULL ); Create( ); getch( ); //关闭事件 CloseHandle( g_hEvent ); return 0;}
转载地址:http://owxui.baihongyu.com/