settimer
// MyThreadTimer.h
#pragma once
class MyThreadTimer
: public CWinThread
{
public:
MyThreadTimer(void);
~MyThreadTimer(void);
public:
bool start(int second);
void stop();
private:
virtual int Run();
private:
virtual void onTimer(WPARAM wParam, LPARAM lParam);
virtual void onStartTimer(WPARAM wParam, LPARAM lParam);
virtual void onStopTimer(WPARAM wParam, LPARAM lParam);
private:
UINT_PTR _timerID;
};
// MyThreadTimer.cpp
#include "stdafx.h"
#include "MyThreadTimer.h"
#define WM_MY_TIMER (WM_USER + 1)
#define WM_MY_START_TIMER (WM_USER + 2)
#define WM_MY_STOP_TIMER (WM_USER + 3)
BEGIN_MESSAGE_MAP(MyThreadTimer, CWinThread)
ON_THREAD_MESSAGE(WM_MY_TIMER, MyThreadTimer::onTimer)
ON_THREAD_MESSAGE(WM_MY_START_TIMER, MyThreadTimer::onStartTimer)
ON_THREAD_MESSAGE(WM_MY_STOP_TIMER, MyThreadTimer::onStopTimer)
END_MESSAGE_MAP()
VOID CALLBACK timerFun(HWND wnd, UINT msg, UINT_PTR id, Dword d)
{
dword threadID = GetCurrentThreadId();
PostThreadMessage(threadID, WM_MY_TIMER, 0, 0);
}
MyThreadTimer::MyThreadTimer(void)
:_timerID(NULL)
{
}
MyThreadTimer::~MyThreadTimer(void)
{
}
bool MyThreadTimer::start(int second)
{
if (m_hThread != NULL)
{
}
m_bAutoDelete = FALSE;
CreateThread();
PostThreadMessage(WM_MY_START_TIMER, (WPARAM)second, 0);
return true;
}
void MyThreadTimer::stop()
{
if (m_hThread == NULL)
{
return;
}
PostThreadMessage(WM_MY_STOP_TIMER, 0, 0);
PostThreadMessage(WM_QUIT, 0, 0);
WaitForSingleObject(m_hThread, INFINITE);
}
BOOL MyThreadTimer::InitInstance()
{
return TRUE;
}
int MyThreadTimer::Run()
{
return CWinThread::Run();
}
void MyThreadTimer::onTimer(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox(_T("定时器时间到了"));
}
void MyThreadTimer::onStartTimer(WPARAM wParam, LPARAM lParam)
{
_timerID = SetTimer(NULL, 0, int(wParam) * 1000, timerFun);
}
void MyThreadTimer::onStopTimer(WPARAM wParam, LPARAM lParam)
{
KillTimer(NULL, _timerID);
}
相关阅读
原文地址 1,只执行一次的定时器 <script> //定时器 异步运行 function hello(){ alert("hello"); } //使用方法名字执行
package com.wxltsoft.tool; import org.junit.Test; import java.util.Calendar; import java.util.Date; import java.util.T
JS设置定时器和清除定时器 在做项目中难免会碰到需要实时刷新,动画依次出现等等需求,这时候就需要定时器登上我们的代码舞台了,所
文章目录任务一:主要方法:程序:运行结果:任务二:主要方法:程序:运行结果:任务三:主要方法:程序:运行结果:这篇博客介绍java的定时器类Timer,
Timer主要用于Java线程里指定时间或周期运行任务,它是线程安全的,但不提供实时性(real-time)保证。 上面提到了守护线程的概念。 J