拥有主消息循环的worker线程
来源:互联网 发布:钢结构制图软件 编辑:程序博客网 时间:2024/06/10 03:21
/*Demonstrate using worker threads that
have their own message queue but no window.*/
#define WIN32_LEAN_AND_MEAN
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<process.h>
#include<string.h>
#include "MeVVerify.h"
unsigned WINAPI ThreadFunc(void* p);
HANDLE ghEvent;
#define WM_JOB_PRINT_AS_IS WM_APP+0x0001
#define WM_JOB_PRINT_REVERSE WM_APP+0x0002
#define WM_JOB_PRINT_LOWER WM_APP+0x0003
int main(VOID)
{
HANDLE hThread;
unsigned tid;
//Give the new thread something to talk to us with
ghEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
hThread = (HANDLE)_beginthreadex(NULL, 0,
ThreadFunc, 0, 0, &tid);
MTVERIFY(hThread);
//This thread has to wait for the new thread
//to init its globals and msg queue.
WaitForSingleObject(ghEvent,INFINITE);
//the only place in the book we get to use the thread ID
char *szText = strdup("Thank you for buying this book.\n");
PostThreadMessage(tid, WM_JOB_PRINT_AS_IS, NULL,
(LPARAM)szText);
szText = strdup("Text is easier to read forward.\n");
PostThreadMessage(tid, WM_JOB_PRINT_REVERSE, NULL, (LPARAM)szText);
szText = strdup("\n LOWER CASE IS FOR WHISPERING.\n");
PostThreadMessage(tid, WM_JOB_PRINT_LOWER, NULL, (LPARAM)szText);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
return 0;
}
VOID CALLBACK TimerFunc(
HWND hwnd,//handle of window for timer message
UINT uMsg,//WM_TIMER message
UINT idEvent,//timer identifier
DWORD dwTime//current system time
)
{
UNREFERENCED_PARAMETER(hwnd);
UNREFERENCED_PARAMETER(uMsg);
PostThreadMessage(GetCurrentThreadId(), WM_QUIT, 0, 0);
}
/*call a function to do something that terminates
the thread with ExitThread instead of returning */
unsigned WINAPI ThreadFunc(LPVOID n)
{
UNREFERENCED_PARAMETER(n);
MSG msg;
//This creates the message queue
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
SetEvent(ghEvent);
//We'll run for two seconds
SetTimer(NULL, NULL, 2000, (TIMERPROC)TimerFunc);
while (GetMessage(&msg, NULL, 0, 0))
{
char *psz = (char*)msg.lParam;
switch (msg.message)
{
case WM_JOB_PRINT_AS_IS:
printf("%s", psz);
free(psz);
break;
case WM_JOB_PRINT_REVERSE:
printf("%s", strrev(psz));
break;
case WM_JOB_PRINT_LOWER:
printf("%s", _strlwr(psz));
free(psz);
break;
default:
DispatchMessage(&msg);
}
}
return 0;
}
0 0
- 拥有主消息循环的worker线程
- Worker线程使用消息循环
- 线程消息循环与通常消息循环的一点区别
- MFC 子线程消息循环的实现
- HandlerThread:带消息循环(Looper)的线程
- 带消息循环的线程(LooperThread)模板
- Android应用程序线程的消息循环模型
- h5 真正的线程worker
- Android 线程消息循环
- 线程之--- 消息循环
- 009-storm中worker的消息通信
- worker线程建立方法的区别
- worker线程操作界面引起的死锁
- CPrcThread<Worker>线程的挂起和恢复
- AfxBeginThread: Worker线程的创建和结束
- H5 web worker线程的使用
- 工作线程与消息循环
- Android 线程消息循环机制
- 浅谈Vivado HLS存储结构中Line Buffer
- 什么叫编译时和运行时
- MathType中真子集的输入技巧
- 每天一道算法题(26)——输入字符串表达式求值
- String,StringBuffer,StringBuild的区别
- 拥有主消息循环的worker线程
- nexus管理maven部署
- 会场安排问题
- JS正则表达式验证数字非常全
- 黑马程序员——java中数组的定义与应用
- unity3d ppsspp模拟器中的post processing shader在unity中使用
- 除了喜怒哀乐外还有许多的感情我们难以用语言文字表述
- LeetCode234——Palindrome Linked List,O(n) time and O(1) space
- 模拟windows消息框