设置多媒体定时器的起始函数为成员函数!
来源:互联网 发布:通信达的主力抄底源码 编辑:程序博客网 时间:2024/04/30 03:47
为了避免使用Sleep,最好能够另起一个线程,但是线程的处理函数如果是全局函数,则就不能调用和处理类的成员变量和成员函数,为了使能够在线程中处理成员变量和成员函数,必须通过一定的转换,下面就是我做的例子!
// ThreadObject.h: interface for the CThreadObject class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__INCLUDED_)
#define AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef unsigned (WINAPI * THREADFUNC)(LPVOID para);
typedef unsigned * THREADID;
typedef void (CALLBACK TIMECALLBACK)(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
typedef TIMECALLBACK FAR *LPTIMECALLBACK;
#include
#include
typedef struct _tag_data
{
LPVOID thisobject;
} T_DATA;
class CThreadObject
{
public:
static void CALLBACK OneShotTimer(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
void StartThread();
CThreadObject();
virtual ~CThreadObject();
static DWORD WINAPI ThreadFunc(LPVOID param);
protected:
void WaitForExit();
virtual DWORD ThreadMemberFunc();
HANDLE m_hThread;
DWORD m_ThreadId;
};
#endif // !defined(AFX_THREADOBJECT_H__195BAF3F_6D2E_4494_A5EA_C5F0C66F3618__INCLUDED_)
// ThreadObject.cpp: implementation of the CThreadObject class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "testThread.h"
#include "ThreadObject.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CThreadObject::CThreadObject()
{
m_hThread=NULL;
m_ThreadId=0;
}
CThreadObject::~CThreadObject()
{
if (m_hThread)
WaitForExit();
}
#define TARGET_RESOLUTION 1 // 1-millisecond target resolution
void CThreadObject::StartThread()
{
TIMECAPS tc;
UINT wTimerRes;
MMRESULT timeID;
T_DATA tempData;
if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR)
{
// Error; application can't continue.
}
wTimerRes = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
timeBeginPeriod(wTimerRes);
tempData.thisobject=(LPVOID)this;
timeID=timeSetEvent(
100, // delay
wTimerRes, // resolution (global variable)
CThreadObject::OneShotTimer, // callback function
(DWORD_PTR)&tempData, // user data
TIME_ONESHOT ); // single timer event
if(! timeID)
TRACE("错误/n");
else
TRACE("创建成功/n");
TRACE("THIS=%d/n",this);
}
DWORD WINAPI CThreadObject::ThreadFunc(LPVOID param)
{
CThreadObject * pThread=(CThreadObject *)param;
return pThread->ThreadMemberFunc();
}
DWORD CThreadObject::ThreadMemberFunc()
{
TRACE("调用成员变量/n");
return 0;
}
void CThreadObject::WaitForExit()
{
WaitForSingleObject(m_hThread,INFINITE);
CloseHandle(m_hThread);
}
void CALLBACK CThreadObject::OneShotTimer(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
T_DATA * tempData;
TRACE("非常好/n");
TRACE("dwUser=%d/n",dwUser);
TRACE("dw1=%d/n",dw1);
TRACE("dw2=%d/n",dw2);
tempData=(T_DATA * )dwUser;
CThreadObject * pThread=(CThreadObject *)(tempData->thisobject);
pThread->ThreadMemberFunc();
//ThreadMemberFunc();
}
调用顺序
CThreadObject * pThread;
pThread=new CThreadObject();
pThread->StartThread();
- 设置多媒体定时器的起始函数为成员函数!
- 把成员函数作为线程起始函数
- 把成员函数作为线程起始函数
- windows多媒体定时器timeSetEvent()函数
- this 指针的地址--调用成员函数的所在对象的起始地址
- 声明为非成员函数的时机
- C++之成员函数声明为虚函数的规则
- 把成员函数转换为函数的代码。
- C++使用线程函数pthread_create时,调用的成员函数要定义为静态成员函数
- Linux定时器设置函数setitimer
- 类成员函数的函数指针与返回值为函数指针的函数
- 成员函数模板不能为虚函数
- 定时器的专用函数
- 将重载运算符定义为成员函数还是普通的非成员函数
- 数据成员、设置函数及获取函数
- 带成员函数指针的成员函数
- 可恶的多媒体定时器
- 成员函数的函数配接器
- 庆祝
- 【转】好东西就是要多传(关于思恋妈妈的帖子)
- 时间日期格式-国家标准
- 时间日期格式-ISO8601
- 时间日期格式-W3C
- 设置多媒体定时器的起始函数为成员函数!
- 把成员函数作为线程起始函数
- 每个.Net开发人员应该下载的十种必备工具
- 路由器网络接口解析
- Free Philosophy: Part I The Beauty of Doubt
- GRE over IPsec
- 在linux挂载windows文件系统
- 萱草
- 两个书法网站