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

Leo

<2007年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

統計

  • 隨筆 - 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 閱讀(1669) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   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>
            伊人狠狠色j香婷婷综合| 亚洲欧洲一区二区三区在线观看 | 国产欧美日韩一区二区三区在线观看| 日韩网站在线观看| 亚洲精品国产日韩| 欧美日韩麻豆| 午夜国产精品影院在线观看 | 欧美成人国产一区二区| 久久亚洲私人国产精品va| 亚洲激情黄色| 亚洲免费成人av电影| 国产精品美女久久福利网站| 久久av一区二区三区| 久久尤物电影视频在线观看| 99re成人精品视频| 午夜欧美精品| 亚洲精品久久久久久久久久久久| 亚洲精品欧美在线| 国产在线观看精品一区二区三区| 欧美电影在线| 国产精品一二三四区| 美女视频黄 久久| 欧美日韩一区在线播放| 久久在线播放| 欧美日韩一二三区| 久久久www免费人成黑人精品 | 欧美日韩视频免费播放| 欧美在线亚洲综合一区| 欧美大秀在线观看| 欧美在线影院在线视频| 欧美国产极速在线| 久久久久国产成人精品亚洲午夜| 欧美精品福利| 久久一二三区| 国产乱码精品1区2区3区| 亚洲国产一二三| 红桃视频亚洲| 亚洲欧美久久久久一区二区三区| 91久久亚洲| 久久精品一区二区三区不卡| 亚洲专区一二三| 欧美激情综合亚洲一二区| 久久久久久久一区二区| 国产精品久久久| 亚洲欧洲久久| 亚洲欧洲精品一区| 久久精品中文字幕一区| 午夜久久一区| 欧美日韩综合在线免费观看| 欧美77777| 激情视频一区| 久久精彩免费视频| 久久久国产一区二区三区| 国产精品免费久久久久久| 亚洲精品一级| 在线一区亚洲| 欧美日韩成人一区二区三区| 亚洲成人资源网| 亚洲欧美在线x视频| 这里只有精品视频| 欧美日韩美女在线| 亚洲精品乱码久久久久| 亚洲精品免费电影| 欧美黄色视屏| 亚洲人在线视频| 宅男噜噜噜66一区二区 | 欧美专区日韩专区| 久久精品国产免费| 国产午夜精品全部视频播放 | 亚洲精品视频在线观看免费| 亚洲欧洲一区二区三区| 欧美国产一区二区在线观看 | 亚洲男人第一网站| 午夜精品一区二区三区四区| 国产精品免费福利| 午夜精品久久久99热福利| 欧美资源在线| 一区二区在线视频播放| 久久资源在线| 亚洲激情视频| 亚洲欧美国产精品va在线观看| 国产精品家庭影院| 欧美一区二区三区免费视| 美女精品网站| 一本大道av伊人久久综合| 欧美性色视频在线| 欧美一站二站| 亚洲国产精品传媒在线观看| 亚洲视频国产视频| 国产亚洲精品aa午夜观看| 久久综合一区二区| 一本大道久久a久久综合婷婷| 销魂美女一区二区三区视频在线| 国产一区二区视频在线观看| 美日韩精品免费观看视频| 亚洲看片免费| 久久亚裔精品欧美| 亚洲网站在线| 精品成人久久| 国产精品成人在线观看| 久久久久久久综合狠狠综合| 亚洲久久视频| 蜜臀av性久久久久蜜臀aⅴ四虎| 亚洲免费观看高清完整版在线观看| 国产精品二区影院| 久热爱精品视频线路一| 一区二区三区福利| 欧美黄色网络| 欧美在线亚洲在线| 99精品黄色片免费大全| 狠狠色狠狠色综合日日91app| 欧美理论在线| 久久躁日日躁aaaaxxxx| 亚洲欧美日韩精品| 91久久综合| 免费中文字幕日韩欧美| 亚洲欧美日韩国产| 日韩一区二区精品葵司在线| 国产亚洲美州欧州综合国| 欧美日韩一二三四五区| 久久视频在线看| 先锋亚洲精品| 亚洲欧美春色| 中文av字幕一区| 日韩视频在线一区二区三区| 蜜桃av一区二区三区| 久久精品视频一| 亚洲欧洲av一区二区| 洋洋av久久久久久久一区| 在线 亚洲欧美在线综合一区| 国产精品一区二区久久久| 欧美午夜a级限制福利片| 欧美丰满高潮xxxx喷水动漫| 久久免费偷拍视频| 久久久久久久综合日本| 久久精品1区| 久久精品人人爽| 欧美在线一二三| 久久av免费一区| 久久国产免费| 久久视频一区| 裸体一区二区三区| 麻豆91精品91久久久的内涵| 久久精品二区| 久久午夜av| 麻豆成人综合网| 欧美高清视频一区| 欧美日本在线看| 欧美香蕉视频| 国产精品一区二区a| 国产麻豆91精品| 国产一区激情| 亚洲二区在线视频| 亚洲人成网站999久久久综合| 亚洲精品久久久久久久久久久久 | 亚洲一区二区三区视频播放| 一本色道久久综合亚洲精品婷婷 | 国产精品亚洲成人| 国产女主播在线一区二区| 国产综合视频在线观看| 在线成人黄色| 日韩网站在线| 欧美一区二区三区日韩| 久久久久国产精品午夜一区| 麻豆精品网站| 亚洲毛片播放| 亚洲男人的天堂在线观看| 久久精品99无色码中文字幕| 久久精品夜色噜噜亚洲a∨| 蜜桃av一区二区| 欧美日韩在线精品| 国产在线视频欧美| 亚洲精品国产系列| 亚洲欧美美女| 欧美成人精品h版在线观看| 亚洲国产欧美一区二区三区丁香婷| 亚洲精品在线视频观看| 亚洲欧美日韩一区二区在线| 久久影音先锋| 国产精品女同互慰在线看| 激情成人在线视频| 在线视频精品| 久久国产欧美精品| 欧美视频精品在线| 在线观看日韩国产| 香蕉av777xxx色综合一区| 欧美成人免费va影院高清| 亚洲少妇自拍| 欧美顶级少妇做爰| 国产日产欧产精品推荐色| 亚洲精品国产拍免费91在线| 欧美一级视频| 99热在线精品观看| 麻豆精品在线观看| 国产欧美一级| 亚洲欧美乱综合| 亚洲精品乱码久久久久久蜜桃麻豆| 午夜在线一区二区| 国产精品人人做人人爽| 日韩视频在线你懂得|