Win API 和 MFC 创建的线程中实现设置定时器
来源:互联网 发布:淘宝详情页 太模糊 编辑:程序博客网 时间:2024/06/06 17:58
1、通过CWinThread::CreateThread()创建线程
#include <afxwin.h>
#include <iostream>
UINT m_TimerId;
class ThreadClass : public CWinThread
{
public:
ThreadClass() { };
virtual BOOL InitInstance()
{
m_TimerId = ::SetTimer(NULL, 0, 1000, ThreadClass::TimerProc);
return TRUE;
}
virtual int ExitInstance()
{
::KillTimer(NULL, m_TimerId) ;
return CWinThread::ExitInstance();
}
virtual int Run()
{
return CWinThread::Run();
}
static VOID CALLBACK ThreadClass::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
cout << "定时器执行了!" << GetCurrentTime() << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
ThreadClass* ptest = new ThreadClass;
ptest->CreateThread();
while (true) {};
return 0;
}
ptest->CreateThread()后首先线程类的InitInstance()函数,如果返回TRUE,继续调用线程的Run()函数,该函数的作用是运行一个标准的消息循环,并且当收到WM_QUIT消息后中断,在消息循环过程中,Run()函数检测到线程空闲时(没有消息),也将调用OnIdle()函数,最后Run()函数返回,MFC调用ExitInstance()函数清理资源。
2、通过Windows API 函数CreateThread()创建线程
#include "stdafx.h"
UINT m_TimerId;
DWORD WINAPI ThreadProc(LPVOID lpParameter);
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime)
{
cout << "定时器执行了!" << GetCurrentTime() << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE m_hWndThread;
m_hWndThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
CloseHandle(m_hWndThread);
while (true) {};
return 0;
}
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
m_TimerId = ::SetTimer(NULL, 0, 1000, TimerProc);
MSG msg;
while (true)
{
if (PeekMessage(&msg, 0, WM_TIMER, WM_TIMER, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
- Win API 和 MFC 创建的线程中实现设置定时器
- Win API 和 MFC 创建的线程中实现设置定时器
- MFC 中设置定时器Timer的方法
- MFC中如何安全的创建和关闭线程
- MFC中创建简单定时器
- MFC中线程创建的一般方法
- MFC中工作线程的创建
- javascript中线程和定时器的理解
- Win API中SendMessage和PostMessage的区别
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- MFC多线程的创建,包括工作线程和用户界面线程
- Android系统system用户权限和root权限的获取
- OpenCV Mat结构关键特性记录
- 简单网络请求+ListView
- windows下ruby安装环境配置
- Windows 下安装 express
- Win API 和 MFC 创建的线程中实现设置定时器
- struts2_day02_05-获取表单数据(使用ActionContext获取)
- 1、编写一个Java程序在屏幕上输出“你好!”。
- Android 开发的17个建议,紧跟Google官方,进阶必看!
- ROS launch文档介绍
- Excel真是太神奇了
- 前端开发者不得不知的 ECMAScript 6 十大特性
- 7-3 排座位
- javascript学习要点