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

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>
            亚洲欧洲美洲综合色网| 久久午夜色播影院免费高清| 日韩视频不卡| 亚洲国产岛国毛片在线| 美女91精品| 久久综合九色综合网站| 亚洲免费av观看| 亚洲色图综合久久| 国产午夜精品全部视频在线播放| 欧美中文在线免费| 久久综合给合| 亚洲视频一区| 午夜精品亚洲一区二区三区嫩草| 黄色精品一二区| 亚洲人成网站精品片在线观看| 欧美日韩国产在线看| 欧美在线观看视频| 老妇喷水一区二区三区| 在线一区二区日韩| 欧美在线观看一区二区三区| 亚洲第一狼人社区| 一本一本久久| 国产精品久久久久久久久久三级| 野花国产精品入口| 久久婷婷国产综合国色天香 | 久久久久久亚洲综合影院红桃 | 亚洲美女色禁图| 国产视频在线观看一区二区| 亚洲国产精品www| 国产欧美一级| 亚洲精品日韩在线观看| 有码中文亚洲精品| 亚洲一区二区成人| 亚洲精品免费看| 久久久综合视频| 亚洲欧美一区二区原创| 欧美激情一区三区| 老司机免费视频一区二区三区| 欧美日韩在线视频首页| 欧美高清hd18日本| 韩国久久久久| 亚洲在线视频| 亚洲伊人色欲综合网| 欧美刺激午夜性久久久久久久| 久久亚洲影音av资源网| 国产精品私房写真福利视频 | 欧美一级大片在线观看| 欧美日韩国产精品一区二区亚洲| 久久综合伊人77777麻豆| 国产精品一区在线观看| 99re6这里只有精品| 亚洲精品网站在线播放gif| 久久久久www| 久久久久se| 国产午夜精品麻豆| 亚洲欧美久久久久一区二区三区| 亚洲一区二区精品| 欧美日韩国产综合视频在线| 亚洲国产高清一区| 亚洲欧洲另类国产综合| 免费不卡在线观看av| 免费观看在线综合色| 黄色一区二区三区| 久久一区欧美| 欧美电影打屁股sp| 最新国产成人在线观看| 欧美α欧美αv大片| 欧美激情片在线观看| 亚洲欧洲精品一区二区精品久久久| 久久久视频精品| 欧美电影美腿模特1979在线看| 在线播放视频一区| 美女性感视频久久久| 亚洲电影毛片| 中日韩男男gay无套| 欧美色另类天堂2015| 亚洲男人的天堂在线观看| 性久久久久久| 伊人天天综合| 欧美sm视频| 一区二区三区日韩在线观看| 亚洲欧美一区二区三区久久| 国产综合亚洲精品一区二| 久久伊人亚洲| 99re6热只有精品免费观看| 性18欧美另类| 亚洲激情婷婷| 国产精品久久一卡二卡| 欧美一区二区视频97| 欧美激情第1页| 亚洲欧美一区在线| 狠狠久久五月精品中文字幕| 欧美第一黄色网| 亚洲一区久久久| 欧美福利一区| 亚洲欧美日韩久久精品| 伊人狠狠色j香婷婷综合| 欧美日韩视频一区二区三区| 欧美一二三区在线观看| 亚洲经典在线看| 久久久不卡网国产精品一区| 日韩视频第一页| 国产一区二区三区av电影 | 美日韩免费视频| 亚洲女同精品视频| 亚洲欧洲一区二区在线播放| 欧美一区午夜精品| 亚洲精选在线观看| 国产午夜亚洲精品羞羞网站 | 欧美一区二区三区男人的天堂| 亚洲国产第一| 美女国产精品| 欧美在线免费| 亚洲午夜久久久| 91久久国产自产拍夜夜嗨| 国产视频久久| 国产精品jvid在线观看蜜臀 | 亚洲自拍偷拍视频| 亚洲日韩中文字幕在线播放| 久久香蕉国产线看观看av| 亚洲欧美日韩视频一区| 一本久久综合亚洲鲁鲁| 亚洲国产精品久久精品怡红院| 国产精品午夜久久| 国产精品久久久久久av下载红粉| 免费一级欧美片在线观看| 久久精品国产99| 先锋资源久久| 亚洲一区二区在线| 一本色道婷婷久久欧美| 99国产精品久久久| 亚洲精品美女在线观看| 亚洲春色另类小说| 欧美国产精品日韩| 欧美96在线丨欧| 美女国产一区| 免费欧美在线| 欧美激情亚洲综合一区| 欧美mv日韩mv亚洲| 欧美激情四色| 亚洲经典一区| 一本一本久久a久久精品牛牛影视| 日韩亚洲欧美一区| 亚洲一二区在线| 亚洲男人的天堂在线观看| 午夜精品理论片| 欧美一区二区大片| 久久狠狠一本精品综合网| 久久久国产91| 欧美成人乱码一区二区三区| 欧美国产精品久久| 欧美日韩三级| 国产精品视频第一区| 国产视频亚洲精品| 一区在线视频观看| 亚洲精品小视频| 亚洲在线视频观看| 久久激情网站| 亚洲成人在线网| 夜夜嗨av色综合久久久综合网| 99热免费精品| 小处雏高清一区二区三区| 久久久欧美一区二区| 欧美精品日韩| 国产伦精品一区二区三区免费| 狠狠色丁香久久综合频道| 亚洲国产精品999| 亚洲午夜三级在线| 久久五月天婷婷| 亚洲日本va午夜在线电影| 亚洲在线中文字幕| 久久午夜色播影院免费高清| 欧美区国产区| 韩国av一区二区| 一本久久综合| 久久久免费观看视频| 亚洲激情视频在线| 欧美一区二区三区免费大片| 欧美黄免费看| 国内精品国产成人| 一区二区三区精品| 美女图片一区二区| 亚洲系列中文字幕| 欧美成人免费网站| 韩国欧美一区| 午夜精品成人在线| 亚洲国产精品精华液2区45| 亚洲永久精品国产| 欧美精品一区二区在线播放| 国产一区清纯| 午夜久久久久| 亚洲精品欧美激情| 久久综合中文| 国产一区二区高清不卡| 一区二区三区视频观看| 欧美大胆人体视频| 欧美资源在线观看| 国产欧美高清| 午夜精品福利视频| 一区二区高清|