青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Leo

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統計

  • 隨筆 - 2
  • 文章 - 0
  • 評論 - 2
  • 引用 - 0

常用鏈接

留言簿(1)

隨筆檔案

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

How to use SetTimer() with callback to a non-static member function

Quick update...

After reviewing the comments and suggestions from a few people, I made the solution better. Look for an update to this article which uses a better approach, namely using the functions:

  • CreateWaitableTimer()
  • SetWaitableTimer()
  • WaitForMultipleObjects()

The solution based on these functions will allow multiple instances of the CSleeperThread class to run (instead of just one using the current example). So stay tuned, I'll have this article updated as soon as possible. :-)

Introduction

I have seen many questions on the boards about how to properly use SetTimer(). I've also noticed that most of these questions are around how to put a thread to sleep for X seconds. One obvious answer would be to use the Sleep() function. The main drawback is, how do you gracefully shut down your thread, or cancel the Sleep() operation before the time expires.

This article is meant to address all of the above. I give an example of putting a thread to sleep using SetTimer(). The SetTimer() calls back to a non-static function. This is key, because normally you have to pass a static member to SetTimer() which means it can't access any other non-static variables or member functions of the class.

Details

Since implementing a non-static callback member is key to this, we'll go into this first. Implementing a callback to a static member function doesn't require anything different from implementing a regular C callback function. Since static member functions have the same signature as C functions with the same calling conventions, they can be referenced using just the function name.

Making a non-static callback member function is a different story, because they have a different signature than a C function. To make a non-static member function, it requires the use of two additional items:

  • A global (void*) pointer, referencing the class of the callback function
  • A wrapper function which will be passed to SetTimer()

This is actually a fairly simple implementation. First, you need to define your class:

class CSleeperThread : public CWinThread {
public:
static VOID CALLBACK TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD dwTime );
VOID CALLBACK TimerProc( HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime );
void ThreadMain();
void WakeUp();
private:
static void * pObject;
UINT_PTR pTimer;
CRITICAL_SECTION lock;
};

Then, don't forget to include the following line in your class implementation file:

void * CSleeperThread::pObject;

Now that we have our class declared, we can look at the wrapper function, the non-static member function and the member function that will call SetTimer():

VOID CALLBACK CSleeperThread::TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD dwTime ) {
CSleeperThread *pSomeClass = (CSleeperThread*)pObject; // cast the void pointer
pSomeClass->TimerProc(hwnd, uMsg, idEvent, dwTime); // call non-static function
}

The wrapper function first initializes a CSleeperThread pointer with pObject. Since pSomeClass is a local pointer, we can access it within the static wrapper function.

VOID CALLBACK CSleeperThread::TimerProc(HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime) {
::EnterCriticalSection(&lock);
if(idEvent == pTimer) {
KillTimer(NULL, pTimer);  // kill the timer so it won't fire again
ResumeThread();  // resume the main thread function
}
::LeaveCriticalSection(&lock);
}

The TimerProc member function isn't static, so we can access other non-static functions like ResumeThread() and we can access the private variable lock. Notice that I've entered a critical section which prevents a second timer event to enter the callback, thus ensuring that the first execution of TimerProc() will cancel out the timer.

Next, let's take a look at the main execution function, ThreadMain().

void CSleeperThread::ThreadMain()
{
pObject = this; // VERY IMPORTANT, must be initialized before
// calling SetTimer()
// call SetTimer, passing the wrapper function as the callback
pTimer = SetTimer(NULL, NULL, 10000, TimerProc_Wrapper);
// suspend until the timer expires
SuspendThread();
// the timer has expired, continue processing 
}

The first step in ThreadMain() is absolutely critical. We need to assign the class instance pointer (this) to the pObject variable. This is how the wrapper callback function will gain access to execute the non-static member function.

Next, we just call SetTimer() passing in a function pointer to our wrapper function. SetTimer() will call the wrapper function when the timer expires. The wrapper function in turn, will execute the non-static function TimerProc(), by accessing the static variable pSomeClass.

NOTE: I chose to implement a main function that will create the timer, go to sleep, continue processing and then exit when finished. This is in effect a function that will only execute once per timer. You could easily add a loop to ThreadMain() which would execute once for each timer event.

One last little function. Since we used SuspendThread() in ThreadMain(), if we need to wake up the thread (for whatever reason), all we have to do is make a call to ResumeThread(). So, I've added an access function like so:

void WakeUp() {
::EnterCriticalSection(&lock);
KillTimer(NULL, pTimer);
ResumeThread(); // wake the thread up
}

Buh dee buh dee, that's all folks...

And there we have it. A thread safe class that goes to sleep using SetTimer() and a non-static callback function; which also has the ability to wake up before the timer expires.

Hopefully, you have found this helpful. I've actually used this code in a project I'm working on now, and was in hopes someone else would get some good use out of it.

Someone once told me "you'll like programming if you like banging your head against the wall repeatedly". I've found that to be true, it took me literally several days to figure out what I've put into this article, I'm just slow I guess.

Whew, my head hurts, time for some Advil...or Ibooprofin.. or asssprin.... or something.

Credits...

I probably learned way more in the process of writing this article. So, much thanks goes to Lars Haendel for creating a web-site dedicated to understanding function pointers, without which I wouldn't know didley.

www.function-pointer.org.

posted on 2007-05-16 10:31 LeoChen 閱讀(1658) 評論(0)  編輯 收藏 引用

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            这里只有精品在线播放| 欧美视频精品在线| 欧美人在线视频| **网站欧美大片在线观看| 欧美亚洲综合网| 国产精品99久久久久久久久久久久| 亚洲精品一二三| 噜噜噜91成人网| 久久久精品国产免大香伊| 国产午夜精品全部视频在线播放| 亚洲男人av电影| 亚洲一区二区在线播放| 极品尤物av久久免费看| 美女黄网久久| 欧美日韩国产精品一区| 亚洲视频一区二区| 亚洲午夜精品久久| 国产一区二区三区久久| 久久久噜久噜久久综合| 久久躁日日躁aaaaxxxx| 亚洲精品国产精品久久清纯直播| 亚洲三级免费| 国产精品国产a| 久久精品国产欧美激情| 久久中文久久字幕| 亚洲精品一区在线| 亚洲欧美视频在线观看视频| 国产中文一区二区| 欧美福利视频一区| 欧美日韩一区二区三区免费看| 欧美成人激情视频免费观看| 在线亚洲精品| 欧美影院精品一区| 亚洲美女黄网| 亚洲欧美精品中文字幕在线| 亚洲国产日韩欧美在线99| 日韩网站在线观看| 国产亚洲激情视频在线| 亚洲韩国一区二区三区| 国产精品一区在线观看| 亚洲日本激情| 国产一区在线看| 91久久精品视频| 国产亚洲精品久| 亚洲人成人99网站| 黄网站色欧美视频| 亚洲网站在线| 亚洲欧洲午夜| 西瓜成人精品人成网站| 亚洲伦理中文字幕| 久久精品毛片| 午夜欧美理论片| 欧美精品123区| 免费观看日韩av| 国产乱码精品一区二区三区av| 亚洲第一精品福利| 韩国三级电影一区二区| 亚洲一区二区在线观看视频| 日韩视频在线一区二区三区| 久久精品av麻豆的观看方式| 亚洲宅男天堂在线观看无病毒| 欧美承认网站| 噜噜噜噜噜久久久久久91| 亚洲精品视频一区| 在线观看中文字幕亚洲| 午夜在线精品| 久久国产福利| 国产精品―色哟哟| 宅男噜噜噜66一区二区66| 亚洲理论在线观看| 久热精品在线| 欧美激情欧美激情在线五月| 国产综合在线看| 午夜精品免费视频| 性欧美大战久久久久久久免费观看| 欧美日韩18| 99av国产精品欲麻豆| 亚洲精品欧美专区| 亚洲一二三区精品| 午夜亚洲性色福利视频| 欧美三级视频在线播放| 免费的成人av| 国产欧美成人| 欧美亚洲一区二区三区| 久久精品人人做人人爽| 国内精品国产成人| 久久久久久婷| 欧美大片在线看免费观看| 亚洲国产成人午夜在线一区| 美日韩精品免费| 欧美福利视频在线| 日韩视频在线一区| 欧美日韩亚洲另类| 亚洲一区二区三区中文字幕在线| 性欧美1819sex性高清| 国产精品中文字幕欧美| 久久成人亚洲| 亚洲福利视频一区二区| 一区二区久久| 国产精品永久免费观看| 久久久精品2019中文字幕神马| 欧美大片在线观看一区二区| 亚洲男同1069视频| 国外成人网址| 91久久精品国产| 久久久国产一区二区| 亚洲国产另类久久久精品极度| 欧美国产日韩在线| 亚洲无线视频| 美日韩精品视频免费看| 亚洲视频你懂的| 在线成人欧美| 免费国产自线拍一欧美视频| 夜夜爽99久久国产综合精品女不卡 | 欧美色播在线播放| 性欧美8khd高清极品| 欧美成人激情视频免费观看| 制服丝袜激情欧洲亚洲| 国产最新精品精品你懂的| 男同欧美伦乱| 午夜精品免费| 亚洲精品欧美在线| 久久亚洲风情| 亚洲一区二区三区午夜| 亚洲免费av电影| 黄色一区二区三区四区| 欧美精品v日韩精品v国产精品| 亚洲欧美综合v| 亚洲三级免费| 欧美成人一区二区三区在线观看 | 国产视频亚洲精品| 欧美日韩亚洲高清| 麻豆成人av| 欧美一区二区成人| 99re6热在线精品视频播放速度| 久久久久久久97| 欧美一级片一区| 一区二区日韩免费看| 亚洲成人原创| 国产一区二区三区自拍| 国产精品vvv| 欧美人与性禽动交情品| 你懂的一区二区| 久久精品99无色码中文字幕| 亚洲免费一在线| 午夜视频在线观看一区二区| 中文日韩电影网站| 在线亚洲激情| 99国产一区| 亚洲黄色有码视频| 亚洲国产精品精华液网站| 蜜臀久久久99精品久久久久久| 久久精品一区二区国产| 性欧美18~19sex高清播放| 亚洲欧美日韩国产精品| 亚洲综合欧美日韩| 亚洲淫片在线视频| 亚洲欧美日韩另类| 欧美一级视频精品观看| 午夜在线成人av| 久久av在线| 久久精品亚洲| 麻豆乱码国产一区二区三区| 毛片av中文字幕一区二区| 免费观看国产成人| 亚洲高清资源| 亚洲精品在线观看免费| 一本色道久久综合亚洲精品不卡| 亚洲美女免费视频| 亚洲素人一区二区| 午夜精品久久| 久久综合久色欧美综合狠狠| 欧美另类专区| 国产精品入口日韩视频大尺度| 伊人婷婷久久| 日韩亚洲欧美成人| 亚洲免费小视频| 久久av二区| 欧美激情一区二区三区成人| 亚洲精品乱码久久久久久日本蜜臀 | 欧美二区视频| 亚洲每日更新| 午夜性色一区二区三区免费视频| 久久精品国产999大香线蕉| 欧美 日韩 国产 一区| 欧美日韩第一区日日骚| 国产精品羞羞答答| 亚洲电影在线看| 亚洲影院免费| 麻豆精品视频在线观看视频| 亚洲美女区一区| 玖玖玖免费嫩草在线影院一区| 欧美日韩免费观看一区| 在线国产精品一区| 亚洲一区二区三区在线看| 美女91精品| 亚洲一区二区三区四区五区午夜| 久久久国产91| 欧美性猛片xxxx免费看久爱| 欲香欲色天天天综合和网|