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

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 閱讀(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>
            一区二区三区久久久| 久久久久久久综合日本| 免费成人高清在线视频| 国外成人在线视频| 欧美一区二区三区婷婷月色| 免费亚洲电影在线观看| 久久国产一区二区| 好男人免费精品视频| 久久久久久久一区| 国产综合久久| 欧美性做爰毛片| 亚洲一区亚洲二区| 欧美在线观看一区二区三区| 亚洲国产日韩在线| 亚洲一区二区三| 在线观看不卡| 亚洲视频在线观看一区| 国模一区二区三区| 亚洲精品在线一区二区| 国产欧美一区二区三区在线老狼| 老司机aⅴ在线精品导航| 欧美福利一区二区三区| 久久se精品一区精品二区| 蜜桃视频一区| 久久大逼视频| 欧美日韩视频第一区| 久久综合伊人77777蜜臀| 欧美另类一区二区三区| 久久亚洲图片| 国产精品www网站| 亚洲福利视频三区| 国产欧美日韩一区二区三区在线观看| 欧美bbbxxxxx| 国产麻豆综合| 99国产一区二区三精品乱码| 激情av一区二区| 亚洲视频自拍偷拍| 99re热这里只有精品视频 | 亚洲激情在线观看| 亚洲欧美激情诱惑| 一本大道久久a久久综合婷婷| 久久不射电影网| 亚洲在线中文字幕| 欧美另类在线播放| 亚洲国产一区二区精品专区| 国外成人免费视频| 欧美一二三区精品| 先锋a资源在线看亚洲| 欧美三级不卡| 99国产精品久久久| 亚洲小视频在线| 欧美日韩成人在线| 亚洲精品中文字| 一本一道久久综合狠狠老精东影业| 免费国产一区二区| 欧美高清不卡| 亚洲精品人人| 欧美日本国产视频| 日韩午夜激情av| 宅男精品视频| 欧美调教视频| 亚洲午夜av在线| 欧美亚洲午夜视频在线观看| 国产精品人人爽人人做我的可爱| 一区二区三区欧美在线| 亚洲欧美日韩国产综合在线 | 国产欧美在线视频| 国产午夜精品理论片a级大结局| 在线视频欧美日韩| 性欧美精品高清| 国产自产女人91一区在线观看| 欧美一区二区三区视频在线观看| 久久精品盗摄| 在线欧美一区| 欧美精品久久久久久久久久| 日韩一区二区精品在线观看| 亚洲嫩草精品久久| 国内精品视频在线观看| 久久综合网络一区二区| 最新热久久免费视频| 亚洲影视在线播放| 国产主播喷水一区二区| 欧美插天视频在线播放| 99成人精品| 久久久久久久欧美精品| 亚洲国产网站| 国产精品久久97| 久久香蕉精品| 一区二区高清视频在线观看| 久久国产精品久久精品国产| 亚洲第一页在线| 欧美午夜寂寞影院| 久久综合五月天婷婷伊人| 99国产精品视频免费观看一公开| 欧美亚洲系列| 亚洲精品麻豆| 国产伊人精品| 欧美日韩视频| 久久婷婷国产麻豆91天堂| 亚洲最新中文字幕| 免费中文字幕日韩欧美| 亚洲综合三区| 亚洲国产欧美另类丝袜| 国产精品一区毛片| 欧美精品在线免费| 久久精品人人做人人爽| 一区二区三区成人| 欧美福利网址| 久久精品一区中文字幕| 在线综合欧美| 亚洲精品精选| 红桃视频欧美| 国产毛片一区| 欧美日韩亚洲成人| 男男成人高潮片免费网站| 香蕉久久国产| 亚洲一区美女视频在线观看免费| 欧美黄色影院| 美女999久久久精品视频| 午夜精品电影| 亚洲一区在线直播| 一本色道久久综合亚洲精品不卡 | 亚洲视频网站在线观看| 亚洲国产精品99久久久久久久久| 国产乱人伦精品一区二区| 欧美午夜大胆人体| 欧美激情国产高清| 免费不卡在线观看av| 久久亚洲一区二区| 久久精品国产综合| 欧美在线观看一二区| 香蕉久久夜色精品国产| 99在线精品观看| 日韩一级大片在线| 日韩一级在线| 亚洲天堂第二页| 亚洲性感美女99在线| 一区二区三区成人精品| 亚洲天堂成人| 亚洲欧美日本视频在线观看| 亚洲女性喷水在线观看一区| 亚洲永久视频| 欧美中文在线免费| 亚洲丰满少妇videoshd| 亚洲精品1区2区| 亚洲人成在线播放| 99精品欧美一区二区三区综合在线| 亚洲精品一区二| 夜夜精品视频一区二区| 亚洲午夜视频在线观看| 亚洲欧美清纯在线制服| 性欧美1819性猛交| 久久久久一本一区二区青青蜜月| 久久久精品午夜少妇| 牛牛影视久久网| 亚洲日本中文字幕| 一区二区三区高清在线| 亚洲欧美日韩在线不卡| 欧美一区二区三区免费大片| 久久国产福利国产秒拍| 美国十次了思思久久精品导航| 欧美福利一区二区| 国产精品国色综合久久| 国模精品一区二区三区| 亚洲人成毛片在线播放| 亚洲一区中文| 老牛国产精品一区的观看方式| 亚洲成色777777在线观看影院| 亚洲精品免费一区二区三区| 亚洲欧美日韩国产成人| 久久久噜噜噜久久| 欧美日韩精品一区二区在线播放 | 亚洲国产另类精品专区 | 在线亚洲欧美| 久久视频在线免费观看| 亚洲人妖在线| 久久国产99| 国产精品爱啪在线线免费观看| 国产一区二区三区av电影| 亚洲精品影院在线观看| 欧美专区亚洲专区| 91久久综合| 久久久美女艺术照精彩视频福利播放 | 久久久久一本一区二区青青蜜月| 亚洲人成啪啪网站| 欧美在线免费播放| 国产精品av一区二区| 亚洲成色www久久网站| 亚洲欧美精品在线观看| 亚洲高清视频一区| 久久久福利视频| 国产精品一区二区在线观看不卡| 亚洲区在线播放| 欧美~级网站不卡| 性色一区二区三区| 国产精品video| 夜夜嗨av一区二区三区免费区| 蜜桃av综合| 欧美在线观看一二区| 国产视频亚洲精品|