#include <stdio.h>
#include <windows.h>
#include <conio.h>
UINT cnt = 0;
// 定时器回调函数
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);
// 线程回调函数
DWORD CALLBACK ThreadProc(PVOID pvoid);
// 主函数
int main()
{
DWORD dwThreadId;
// 创建线程
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &dwThreadId);
printf("hello, thread start!\n");
getch(); // 得到键盘输入后再退出
return 0;
}
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
cnt ++;
printf("thread count = %d\n", cnt);
}
DWORD CALLBACK ThreadProc(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
SetTimer(NULL, 10, 1000, TimeProc);
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_TIMER)
{
TranslateMessage(&msg); // 翻译消息
DispatchMessage(&msg); // 分发消息
}
}
KillTimer(NULL, 10);
return 0;
}
#include <windows.h>
#include <conio.h>
UINT cnt = 0;
// 定时器回调函数
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime);
// 线程回调函数
DWORD CALLBACK ThreadProc(PVOID pvoid);
// 主函数
int main()
{
DWORD dwThreadId;
// 创建线程
HANDLE hThread = CreateThread(NULL, 0, ThreadProc, 0, 0, &dwThreadId);
printf("hello, thread start!\n");
getch(); // 得到键盘输入后再退出
return 0;
}
void CALLBACK TimeProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
cnt ++;
printf("thread count = %d\n", cnt);
}
DWORD CALLBACK ThreadProc(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
SetTimer(NULL, 10, 1000, TimeProc);
while(GetMessage(&msg, NULL, 0, 0))
{
if(msg.message == WM_TIMER)
{
TranslateMessage(&msg); // 翻译消息
DispatchMessage(&msg); // 分发消息
}
}
KillTimer(NULL, 10);
return 0;
}
本文展示了一个使用C++实现的线程和定时器应用示例,通过创建线程并设置定时器回调函数,实现了计数器功能。代码中包括了线程创建、线程回调函数定义以及定时器的初始化与销毁过程。
2365

被折叠的 条评论
为什么被折叠?



