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

Leo

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

統計

  • 隨筆 - 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>
            欧美黄色网络| 蜜臀av国产精品久久久久| 一本色道久久88综合日韩精品 | 国产综合色精品一区二区三区| 国内精品久久久久影院薰衣草| 午夜精品久久久久影视| 亚洲高清不卡在线观看| 亚洲无线一线二线三线区别av| 性做久久久久久免费观看欧美| 美女性感视频久久久| 中日韩美女免费视频网址在线观看 | 亚洲欧美日本国产专区一区| 久久xxxx精品视频| 99热这里只有精品8| 麻豆精品网站| 日韩一级在线观看| 欧美福利视频| 欧美一区二区在线免费观看| 欧美日韩一区高清| 日韩视频一区二区三区在线播放| 久久这里只有| 欧美伊人久久| 国产乱码精品一区二区三区av| 亚洲午夜精品视频| 欧美一级欧美一级在线播放| 亚洲国产精品毛片| 欧美大片免费久久精品三p| 欧美国产精品| 亚洲美女视频网| 亚洲专区免费| 国产精品人成在线观看免费| 亚洲一区二区久久| 久久久久9999亚洲精品| 韩国精品在线观看| 久久综合精品国产一区二区三区| 欧美福利小视频| 久久精品国产一区二区电影| 亚洲欧美另类综合偷拍| 亚洲国产精品久久精品怡红院| 亚洲视频免费在线| 亚洲国产日韩美| 亚洲精品国产视频| 欧美日韩久久精品| 亚洲视频精品| 亚洲欧美在线高清| 激情伊人五月天久久综合| 鲁大师成人一区二区三区| 久久久人成影片一区二区三区| 亚洲大片在线观看| 亚洲国产精品电影在线观看| 欧美激情中文字幕乱码免费| 夜夜嗨av一区二区三区四区| 一区二区三区免费网站| 亚洲激情欧美激情| 久久久久久久999| 欧美一区二视频在线免费观看| 欧美在线你懂的| 亚洲欧美久久久| 欧美日韩不卡一区| 亚洲国产美女精品久久久久∴| 国产日韩欧美高清| 欧美激情第3页| 在线观看中文字幕亚洲| 欧美影院午夜播放| 久久爱91午夜羞羞| 国产九色精品成人porny| 日韩小视频在线观看专区| 国产日韩亚洲欧美综合| 亚洲视频一区在线| 亚洲国内高清视频| 久久亚洲精品网站| 午夜久久久久久久久久一区二区| 欧美日韩亚洲另类| 9久re热视频在线精品| 亚洲网站啪啪| 国产精品扒开腿做爽爽爽视频| 麻豆精品视频| 亚洲电影免费观看高清| 久久综合给合久久狠狠色| 亚洲女人天堂成人av在线| 国产精品卡一卡二| 亚洲高清免费在线| 亚洲精品亚洲人成人网| 久久久99精品免费观看不卡| 久久美女性网| 国产伦精品一区二区三区高清版| 亚洲免费婷婷| 蜜桃av一区| 亚洲看片免费| 国产精品久久久999| 亚洲国产高清aⅴ视频| 一区二区日韩| 美腿丝袜亚洲色图| 亚洲日产国产精品| 亚洲欧美高清| 伊人久久综合97精品| 亚洲欧美清纯在线制服| 久久婷婷国产麻豆91天堂| 亚洲第一毛片| 欧美性色视频在线| 久久精品二区三区| 久久嫩草精品久久久久| 亚洲精品一区二区三区婷婷月 | 一区二区三区www| 久久国产高清| 日韩视频免费观看| 国产日韩综合一区二区性色av| 另类酷文…触手系列精品集v1小说| 亚洲片区在线| 久久久久国产精品午夜一区| 国产精品视频九色porn| 久久久人成影片一区二区三区| 亚洲激情影视| 亚洲免费观看高清完整版在线观看熊 | 一区二区三欧美| 美日韩精品视频免费看| 午夜亚洲视频| 亚洲精品美女91| 国产有码在线一区二区视频| 欧美极品影院| 久久久久免费观看| 亚洲在线成人| 久久久久九九九| 亚洲一区二区免费| 亚洲精品在线一区二区| 国语自产精品视频在线看8查询8 | 久久久精品国产一区二区三区| 91久久国产综合久久91精品网站| 欧美一区二区视频97| 亚洲精品在线一区二区| 精品1区2区3区4区| 国产美女精品在线| 国产精品美女久久久| 欧美日韩国产二区| 欧美成人自拍视频| 一区二区三区精品| 亚洲人成亚洲人成在线观看图片| 久热re这里精品视频在线6| 欧美一级网站| 欧美一进一出视频| 西瓜成人精品人成网站| 亚洲一区不卡| 亚洲一区二区免费| 亚洲一区二区三区精品在线观看| 亚洲乱码国产乱码精品精98午夜| 尤物视频一区二区| 亚洲国产mv| 91久久精品日日躁夜夜躁欧美| 欧美日韩在线视频观看| 欧美一区二区三区视频在线观看 | 亚洲毛片av| 久久精品国内一区二区三区| 亚洲欧美日韩另类精品一区二区三区| 在线视频中文亚洲| 国产精品99久久不卡二区| 在线亚洲欧美| 亚洲欧美综合国产精品一区| 亚洲在线第一页| 亚洲国产天堂久久综合| 亚洲精品国产系列| 一本色道婷婷久久欧美| 一区二区三区四区五区精品视频| 一区二区高清视频在线观看| 亚洲视频久久| 先锋影音一区二区三区| 欧美亚洲日本一区| 浪潮色综合久久天堂| 欧美成人黄色小视频| 久久久精品日韩| 欧美大胆成人| 日韩视频免费观看高清完整版| 正在播放亚洲一区| 欧美一区二区啪啪| 你懂的一区二区| 久久免费视频在线观看| 欧美电影免费观看大全| 国产精品黄色| 激情欧美日韩一区| 日韩一区二区福利| 欧美在线黄色| 亚洲高清在线观看| 在线亚洲免费| 久久久福利视频| 欧美日韩国产系列| 国产主播一区二区三区| 一个色综合av| 久久亚洲国产成人| 亚洲精品影视在线观看| 午夜欧美大片免费观看| 亚洲免费小视频| 免费观看成人鲁鲁鲁鲁鲁视频| 欧美视频网站| 在线免费观看日韩欧美| 亚洲欧美精品在线| 亚洲激情啪啪| 久久久久9999亚洲精品| 国产精品日韩在线一区| 亚洲精品字幕| 欧美成黄导航| 久久精品女人天堂|