Set the start function of the multimedia timer as a member function!

xiaoxiao2021-03-06  156

In order to avoid using Sleep, it is best to make another thread, but if the thread is processed, if it is a global function, the member variables and member functions of the class cannot be invoked and processed, in order to process member variables and member functions in the thread, The following is the example of what I did through a certain conversion!

// 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 # prgma overce # endif //_MSC_VER> 1000

TypeDef unsigned (WinApi * threadfunc) (LPVOID PARA); typedef unsigned * threadid;

TypeDef voidback (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_filestatic 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 resolutionvoid CThreadObject :: StartThread () {TIMECAPS tc; UINT wTimerRes; MMRESULT timeID; T_DATA tempData; if (timeGetDevCaps ! (& tc, sizeof (TIMECAPS)) = TIMERR_NOERROR) {// Error; application can not 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 ("Error / N"); Else TRACE ("Create Success / N"); Trace ("THIS =% D / N", THIS);

}

DWORD WINAPI CTHREADOBJECT :: ThreadFunc (lpvoid param) {cthreadObject * pthread = (cthreadObject *) param; return pthread-> threadmemberfunc ();

DWord CthreadObject :: threadmemberfunc () {trace ("call member variable / n"); return 0;}

Void CthreadObject :: waitforcexit () {WaitForsingleObject (m_hthread, infinite); closehandle (m_hthread);}

Void Callback CthreadObject :: OneShottimer (Uint Utimerid, DWORD DW1, DWORD DW2) {T_Data * Tempdata; Trace ("Very Good / 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 ();

Call order

CThreadObject * pthread;

转载请注明原文地址:https://www.9cbs.com/read-128397.html

New Post(0)