sdk中使用cevent类
本帖最后由 chenhui1990421 于 2013-10-12 16:50:37 编辑
#define _WIN32_WINNT 0x0502
#include <afxmt.h>
#define _WIN32_WINNT 0x0502
#include <stdio.h>
DWORD WINAPI myfun1(//声明线程函数
LPVOID lpParameter
);
DWORD WINAPI myfun2(
LPVOID lpParameter
);
int a=0;
CEvent event;//定义全局变量a
int main()
{
event=CEvent(false,false,NULL,NULL);
printf("事件对象\r\n");
HANDLE h1,h2;//定义线程句柄
//event.m_hObject =::CreateEvent(NULL,FALSE,false,NULL);
event.SetEvent();
h1=::CreateThread(NULL,0,myfun1,NULL,0,NULL);//创建线程1
printf("线程1开始运行!\r\n");
h2=::CreateThread(NULL,0,myfun2,NULL,0,NULL);//创建线程2
printf("线程2开始运行!\r\n");
::CloseHandle(h1);//关闭线程句柄对象
::CloseHandle(h2);
::Sleep(100000);//程序睡眠10秒
return 0;
}
CSyncEvent::CSyncEvent(bool bManualReset,bool bInitialState)
{
m_handle=::CreateEvent(NULL,(BOOL)bManualReset,(BOOL)bInitialState,NULL);
}
CSyncEvent::~CSyncEvent(void)
{
if( m_handle )
{
::CloseHandle(m_handle);
}
}
void CSyncEvent::SetEvent()
{
::SetEvent(m_handle);
}
void CSyncEvent::ResetEvent()
{
::ResetEvent(m_handle);
}
bool CSyncEvent::Wait(int nTime)
{
return WAIT_OBJECT_0 == ::WaitForSingleObject(m_handle,nTime);
}