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

Leo

<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

統(tǒng)計(jì)

  • 隨筆 - 2
  • 文章 - 0
  • 評(píng)論 - 2
  • 引用 - 0

常用鏈接

留言簿(1)

隨筆檔案

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

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 閱讀(1670) 評(píng)論(0)  編輯 收藏 引用


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美精品在线一区二区| 亚洲靠逼com| 性欧美xxxx大乳国产app| 欧美午夜精品久久久久久人妖| 99精品99| 99国产精品自拍| 国产精品视屏| 久久亚洲欧洲| 免费人成精品欧美精品| 99ri日韩精品视频| 亚洲精品影视| 亚洲精品免费网站| 欧美日韩在线播放一区| 欧美一区二区三区播放老司机 | 99精品欧美一区二区蜜桃免费| 亚洲电影欧美电影有声小说| 欧美精品v日韩精品v国产精品| 老鸭窝毛片一区二区三区| 一本到高清视频免费精品| 亚洲一区免费观看| 在线欧美日韩国产| 一区二区三区欧美激情| 韩国三级电影一区二区| 亚洲国产另类久久久精品极度| 欧美日一区二区在线观看 | 欧美精品一区二| 亚洲欧美日韩一区二区三区在线| 香蕉久久夜色精品| 亚洲精品久久7777| 亚洲免费在线观看| 亚洲精品少妇| 午夜亚洲福利| 亚洲午夜国产成人av电影男同| 性欧美超级视频| aa级大片欧美三级| 久久久久久尹人网香蕉| 亚洲欧美日韩精品久久久久| 麻豆久久久9性大片| 午夜精品久久久久久99热软件| 久久综合给合久久狠狠色| 亚洲一区二区高清| 欧美高清视频免费观看| 久久九九国产精品| 国产精品免费看片| 亚洲精品一区二区三区99| 狠狠久久亚洲欧美| 亚洲欧美在线一区二区| 亚洲小说欧美另类社区| 欧美国产日韩一区| 女人香蕉久久**毛片精品| 国产一区二区电影在线观看| 中文欧美在线视频| 夜夜嗨av一区二区三区网页| 另类成人小视频在线| 卡通动漫国产精品| 国产一区二区主播在线| 午夜亚洲视频| 欧美亚洲在线观看| 国产精品福利影院| 一区二区三区波多野结衣在线观看| 亚洲人成网在线播放| 另类专区欧美制服同性| 欧美成人性生活| 伊人伊人伊人久久| 久久女同互慰一区二区三区| 国产日韩欧美中文在线播放| 亚洲影院免费观看| 午夜精品亚洲| 国产婷婷色一区二区三区四区| 一区二区三区日韩欧美| 欧美激情第8页| 亚洲精品一区二区三区四区高清| 一本一本久久| 国产精品福利片| 午夜精品一区二区三区四区| 欧美一区三区三区高中清蜜桃| 国产美女在线精品免费观看| 中文亚洲免费| 久久国产加勒比精品无码| 制服诱惑一区二区| 欧美在线播放| 在线观看91精品国产麻豆| 免费美女久久99| 亚洲理论在线观看| 亚洲欧美中文在线视频| 国产亚洲视频在线| 久久综合狠狠综合久久综合88| 欧美搞黄网站| 亚洲在线视频一区| 亚洲一区二区不卡免费| 久久久爽爽爽美女图片| 亚洲国产成人精品久久久国产成人一区| 久久综合九色综合欧美狠狠| 亚洲精品国久久99热| 午夜精品久久久久久久蜜桃app | 亚洲欧美在线看| 国产日韩欧美二区| 美国三级日本三级久久99| 亚洲剧情一区二区| 久久久精品免费视频| 亚洲大片在线观看| 欧美日韩在线电影| 久久精品国产一区二区电影| 亚洲第一成人在线| 欧美在现视频| 亚洲乱码一区二区| 国产欧美日韩一区二区三区在线观看 | 久久亚洲高清| 中日韩高清电影网| 一区二区视频免费完整版观看| 欧美二区不卡| 久久av在线看| 一区二区欧美激情| 亚洲电影网站| 久久综合99re88久久爱| 亚洲一区二区免费视频| 亚洲国产美女精品久久久久∴| 国产精品第一区| 欧美精品色综合| 久久精品国产亚洲一区二区三区 | 美女视频黄免费的久久| 亚洲欧美国产高清va在线播| 亚洲肉体裸体xxxx137| 国内精品视频666| 欧美特黄一区| 欧美日本亚洲视频| 美女国产一区| 久久免费国产精品1| 久久av一区二区三区漫画| 99精品热6080yy久久| 一区二区在线视频| 国产一区二区福利| 国产精品入口夜色视频大尺度| 欧美日韩少妇| 欧美大片专区| 久久手机精品视频| 久久九九精品99国产精品| 亚洲欧美偷拍卡通变态| 亚洲视频日本| 亚洲午夜精品视频| 亚洲一区精彩视频| 亚洲午夜三级在线| 亚洲欧美国产视频| 午夜激情综合网| 欧美在线日韩| 久久精品国产清自在天天线 | 黄色一区二区在线| 国产一区二区三区日韩| 国产日本欧美一区二区三区在线| 国产精品美女久久福利网站| 国产精品久久影院| 国产伦精品一区二区三区四区免费| 国产精品国产精品| 国产精品视频免费观看| 国产伦精品一区二区三区免费迷 | 久久激情综合网| 久久九九有精品国产23| 久久精品二区亚洲w码| 久久午夜电影网| 欧美a级一区| 欧美日韩免费区域视频在线观看| 欧美日韩国产小视频| 国产精品久久久久久久电影| 国产毛片一区| 狠狠色狠色综合曰曰| 亚洲人成艺术| 亚洲一区国产精品| 久久女同互慰一区二区三区| 欧美sm视频| 亚洲美女色禁图| 午夜精品区一区二区三| 久久先锋影音| 欧美午夜寂寞影院| 一区二区视频在线观看| 99精品视频免费观看视频| 午夜国产精品视频| 嫩模写真一区二区三区三州| 亚洲精品极品| 欧美一区二视频| 欧美精品久久久久久久| 国产老肥熟一区二区三区| 亚洲国产欧美在线 | 精品999久久久| 日韩视频在线免费观看| 久久国产精品毛片| 亚洲国产va精品久久久不卡综合| 亚洲小说欧美另类社区| 鲁大师成人一区二区三区| 国产精品久久婷婷六月丁香| 亚洲国产精品免费| 性欧美18~19sex高清播放| 亚洲第一二三四五区| 欧美在线观看视频| 国产精品a久久久久久| 亚洲福利视频免费观看| 久久精品成人一区二区三区蜜臀 | 欧美中文字幕久久| 亚洲人成网在线播放| 久久国产一区二区| 国产精品区二区三区日本|