第三章 事件 windows程序设计 王艳平版
生活随笔
收集整理的這篇文章主要介紹了
第三章 事件 windows程序设计 王艳平版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
///
// EventDemo.cpp文件
#include <stdio.h>
#include <windows.h>
#include <process.h>
HANDLE g_hEvent;
UINT __stdcall ChildFunc(LPVOID);
int main(int argc, char* argv[])
{
HANDLE hChildThread;
UINT uId;
// 創建一個自動重置的(auto-reset events),未受信的(nonsignaled)事件內核對象
g_hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
hChildThread = (HANDLE)::_beginthreadex(NULL, 0, ChildFunc, NULL, 0, &uId);
// 通知子線程開始工作
printf("Please input a char to tell the Child Thread to work: \n");
getchar();
::SetEvent(g_hEvent);
// 等待子線程完成工作,釋放資源
::WaitForSingleObject(hChildThread, INFINITE);
printf("All the work has been finished. \n");
::CloseHandle(hChildThread);
::CloseHandle(g_hEvent);
return 0;
}
UINT __stdcall ChildFunc(LPVOID)
{
::WaitForSingleObject(g_hEvent, INFINITE);
printf(" ?Child thread is working...... \n");
::Sleep(5*1000); // 暫停5秒,模擬真正的工作
return 0;
}
// EventDemo.cpp文件
#include <stdio.h>
#include <windows.h>
#include <process.h>
HANDLE g_hEvent;
UINT __stdcall ChildFunc(LPVOID);
int main(int argc, char* argv[])
{
HANDLE hChildThread;
UINT uId;
// 創建一個自動重置的(auto-reset events),未受信的(nonsignaled)事件內核對象
g_hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
hChildThread = (HANDLE)::_beginthreadex(NULL, 0, ChildFunc, NULL, 0, &uId);
// 通知子線程開始工作
printf("Please input a char to tell the Child Thread to work: \n");
getchar();
::SetEvent(g_hEvent);
// 等待子線程完成工作,釋放資源
::WaitForSingleObject(hChildThread, INFINITE);
printf("All the work has been finished. \n");
::CloseHandle(hChildThread);
::CloseHandle(g_hEvent);
return 0;
}
UINT __stdcall ChildFunc(LPVOID)
{
::WaitForSingleObject(g_hEvent, INFINITE);
printf(" ?Child thread is working...... \n");
::Sleep(5*1000); // 暫停5秒,模擬真正的工作
return 0;
}
總結
以上是生活随笔為你收集整理的第三章 事件 windows程序设计 王艳平版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于网页的个人音乐播放器系统 毕业设计毕
- 下一篇: 多思计组实验实验五、程序计数器实验