设置多媒体定时器的起始函数为成员函数!

来源:互联网 发布:通信达的主力抄底源码 编辑:程序博客网 时间: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();

原创粉丝点击