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

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>
            欧美黄色小视频| 欧美一区二区精品在线| 免费高清在线一区| 蜜臀va亚洲va欧美va天堂| 亚洲国产精品成人| 欧美色一级片| 久久人人爽人人爽爽久久| 亚洲色诱最新| 欧美成人精品不卡视频在线观看 | 欧美与黑人午夜性猛交久久久| 久久久夜精品| 小处雏高清一区二区三区| 亚洲欧洲一区二区在线播放| 国产日韩三区| 国产精品萝li| 欧美日韩精品免费观看| 麻豆精品精华液| 欧美日韩亚洲另类| 欧美精品国产一区| 裸体一区二区三区| 欧美日韩在线一区二区| 国产精品一区二区在线| 欧美日韩亚洲国产一区| 国产亚洲在线| 国产一区二区久久久| 亚洲国产网站| 在线看片成人| 韩国精品在线观看| 国产一区二区三区久久精品| 亚洲国产成人高清精品| 亚洲欧美日韩国产中文| 亚洲欧美中文日韩在线| 亚洲视频在线观看| 中文在线资源观看网站视频免费不卡 | 久久在线免费视频| 久久久av毛片精品| 久久久女女女女999久久| 欧美一区二区三区久久精品| 欧美精品v国产精品v日韩精品| 欧美午夜不卡影院在线观看完整版免费| 欧美精品久久久久久久久老牛影院 | 亚洲欧美日韩中文视频| 亚洲欧美日韩中文播放| 欧美伦理91i| 欧美视频在线视频| 91久久精品一区| 免费亚洲电影在线| 欧美中文字幕在线播放| 国产精品久久毛片a| 一区二区高清视频| 性视频1819p久久| 99精品国产热久久91蜜凸| 亚洲一区二区三区乱码aⅴ| 一区二区三区成人| 午夜精品免费| 亚洲天堂久久| 亚洲国产影院| 国产精品99免费看| 亚洲国产精品专区久久| 久久久久久久久久久成人| 欧美~级网站不卡| 香蕉成人久久| 国产亚洲综合精品| 久久久久久色| 久久久久久久综合狠狠综合| 好看的av在线不卡观看| 久久人91精品久久久久久不卡| 亚洲欧美激情四射在线日 | 亚洲成人在线免费| 狠狠色2019综合网| 免费成人美女女| 久久久精品动漫| 亚洲第一精品福利| 欧美激情国产日韩| 午夜精品免费视频| 国产一区二区剧情av在线| 蜜桃av综合| 欧美理论片在线观看| 亚洲欧美日韩精品一区二区 | 亚洲日本黄色| 久久精品亚洲精品国产欧美kt∨| 欧美二区在线播放| 夜夜爽99久久国产综合精品女不卡 | 亚洲精品视频在线观看网站| 翔田千里一区二区| 国产综合色产在线精品| 亚洲综合清纯丝袜自拍| 欧美国产日产韩国视频| 欧美韩日一区| 午夜精品福利在线| 久久精品中文| 一区二区动漫| 久久久www成人免费毛片麻豆| 日韩性生活视频| 欧美成人久久| 欧美亚一区二区| 男人的天堂亚洲在线| 欧美午夜视频网站| 米奇777在线欧美播放| 国产精品国产成人国产三级| a91a精品视频在线观看| 欧美好骚综合网| 国产精品久久久久久久免费软件 | 久久国产精品久久久久久久久久 | 欧美高清在线精品一区| 欧美在线看片a免费观看| 亚洲主播在线观看| 亚洲激情精品| 欧美自拍丝袜亚洲| 亚洲一区欧美激情| 欧美成人一区在线| 99re6热只有精品免费观看| 亚洲欧美在线网| 99国产精品| 久久亚洲捆绑美女| 久久精品九九| 国产精品久久久久久av下载红粉| 欧美国产激情二区三区| 黑人极品videos精品欧美裸| 亚洲你懂的在线视频| 在线亚洲+欧美+日本专区| 美女亚洲精品| 欧美 日韩 国产 一区| 国模精品娜娜一二三区| 亚洲男人的天堂在线| 午夜精品亚洲| 国产精品久久久久aaaa| 一区二区三区精品视频在线观看| 91久久精品久久国产性色也91| 久久久久久亚洲精品中文字幕 | 久久精品国产91精品亚洲| 欧美一区二区三区精品| 久久国产欧美精品| 欧美有码在线视频| 国产伦理精品不卡| 亚洲免费在线观看视频| 欧美一区二区性| 国产精品视频专区| 欧美成年人视频| 伊人久久久大香线蕉综合直播| 亚洲第一网站| 国产精品一区二区三区乱码| 在线一区视频| 欧美一区二区精品| 国产伦精品一区二区三区在线观看| 亚洲图片欧洲图片av| 亚洲欧美日韩一区二区三区在线| 国产精品成人免费| 香蕉成人久久| 欧美~级网站不卡| 99精品视频一区| 国产精品vip| 香蕉久久国产| 亚洲第一二三四五区| 一区二区三区高清在线| 国产精品久久久久久久久搜平片| 午夜精品久久久久久久蜜桃app| 欧美专区在线播放| 在线观看日韩av| 欧美精品在线播放| 亚洲在线视频观看| 免费的成人av| 亚洲一区二区免费| 欧美日韩国产a| 亚洲免费伊人电影在线观看av| 久久精品首页| 99精品久久久| 经典三级久久| 性做久久久久久免费观看欧美| 久久国产直播| 亚洲伦伦在线| 欧美精品一区二区三区四区| 亚洲桃花岛网站| 老牛嫩草一区二区三区日本| 国产一区二区三区久久久| 欧美va亚洲va香蕉在线| 亚洲作爱视频| 久久久一二三| 亚洲午夜高清视频| 激情综合色综合久久| 欧美日韩在线免费视频| 久久福利视频导航| 在线性视频日韩欧美| 欧美激情日韩| 久久久噜噜噜久久人人看| 亚洲午夜精品国产| 亚洲人在线视频| 国产一区视频网站| 国产精品成人在线观看| 媚黑女一区二区| 久久成人资源| 亚洲综合视频在线| 一区二区久久久久| 亚洲人成艺术| 亚洲已满18点击进入久久| 亚洲第一在线| 黄色精品在线看| 国产欧美一区二区精品婷婷| 欧美一区二区三区日韩| 一本久久综合亚洲鲁鲁五月天|