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

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>
            欧美亚日韩国产aⅴ精品中极品| 久久九九国产精品怡红院| 欧美高清视频www夜色资源网| 在线播放国产一区中文字幕剧情欧美| 久久精品国产第一区二区三区| 亚洲一区二区三区中文字幕| 国产欧美日韩激情| 免费中文字幕日韩欧美| 欧美暴力喷水在线| 亚洲欧美日韩国产综合在线 | 亚洲日本va午夜在线电影 | 欧美一级专区| 久久人91精品久久久久久不卡 | 欧美日韩一区在线视频| 亚洲欧美日韩综合| 久久国产精品久久久久久久久久 | 国产精品白丝jk黑袜喷水| 欧美夜福利tv在线| 久久最新视频| 亚洲欧美偷拍卡通变态| 欧美一区二区视频在线观看| 亚洲精品一级| 欧美亚洲日本网站| 久久久欧美精品sm网站| 亚洲一区二区在线看| 欧美有码在线观看视频| 亚洲精品免费观看| 午夜日韩在线| 亚洲午夜精品17c| 久久久久国产精品厨房| 一区二区三区免费看| 欧美综合第一页| 在线亚洲一区二区| 久久精品亚洲国产奇米99| 亚洲午夜久久久久久久久电影院 | 亚洲国产一区二区精品专区| 国产精品va在线播放| 免费91麻豆精品国产自产在线观看| 欧美精品色一区二区三区| 久久精品视频在线观看| 国产精品sss| 最新国产精品拍自在线播放| 国产曰批免费观看久久久| 亚洲免费av电影| 亚洲精品欧美一区二区三区| 欧美中文字幕在线| 亚洲自拍偷拍麻豆| 欧美日韩美女在线| 亚洲国产精品高清久久久| 国内外成人免费激情在线视频网站 | 亚洲激情第一页| 在线视频国产日韩| 欧美一区二区视频观看视频| 亚洲一区二区免费看| 欧美精品综合| 最新国产精品拍自在线播放| 在线欧美福利| 久久久国产精品亚洲一区| 欧美在线啊v| 国产一区二区av| 欧美一级免费视频| 欧美一区二粉嫩精品国产一线天| 欧美色区777第一页| 亚洲日本成人女熟在线观看| 亚洲伦理久久| 欧美久久久久久久久| 亚洲欧洲日本国产| 亚洲视频日本| 欧美午夜久久| 午夜精品福利在线观看| 久久激情视频| 国产专区一区| 久久伊人免费视频| 91久久国产综合久久| 亚洲美女精品一区| 欧美日韩一区二区三区四区在线观看| 亚洲欧洲免费视频| 亚洲中字黄色| 国产一区二区精品丝袜| 久久久人成影片一区二区三区| 欧美激情精品久久久久| 亚洲精品自在久久| 欧美图区在线视频| 性伦欧美刺激片在线观看| 女人色偷偷aa久久天堂| 欧美日韩国产精品专区| 中文一区字幕| 开心色5月久久精品| 亚洲精品资源| 国产精品网站在线播放| 欧美在线观看网址综合| 欧美国产日本高清在线| 亚洲尤物在线视频观看| 国产一区二区在线观看免费| 免费成人av在线| 在线亚洲欧美视频| 久久久亚洲欧洲日产国码αv| 最新国产拍偷乱拍精品| 国产精品二区在线观看| 欧美一区免费视频| 欧美高清一区二区| 亚洲一区国产视频| 海角社区69精品视频| 欧美激情一区二区| 欧美一区二区视频在线| 欧美国产一区视频在线观看| 亚洲欧美日韩久久精品| 在线观看av不卡| 国产麻豆成人精品| 女女同性精品视频| 亚洲欧美久久| 午夜精品一区二区三区四区| 亚洲级视频在线观看免费1级| 午夜精品久久久久久久99樱桃| 狂野欧美性猛交xxxx巴西| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 亚洲一区精品在线| 亚洲福利国产精品| 欧美日韩精品国产| 久久免费视频这里只有精品| 亚洲欧美成人综合| 亚洲美女啪啪| 亚洲大胆美女视频| 久久久www| 欧美在线三区| av不卡在线看| 亚洲精品国产品国语在线app| 欧美激情按摩| 久久久www| 久久成人18免费网站| 在线亚洲欧美专区二区| 亚洲精品视频在线观看网站| 久久久999国产| 久久精品一区蜜桃臀影院| 亚洲视频一区二区免费在线观看| 亚洲精品免费观看| 欧美日韩视频在线第一区| 欧美成人免费一级人片100| 久久久99爱| 久久精品国产91精品亚洲| 久久成人免费视频| 亚洲欧美一区二区精品久久久| 一区二区三区欧美成人| 亚洲精品护士| 日韩亚洲视频| 欧美国产欧美亚洲国产日韩mv天天看完整 | 国产精品美女久久久久久久 | 欧美专区福利在线| 亚洲欧美激情视频在线观看一区二区三区| 99国产精品99久久久久久粉嫩| 欧美搞黄网站| 亚洲乱码国产乱码精品精天堂| 久久综合综合久久综合| 欧美韩国日本一区| 欧美激情免费在线| 欧美国产精品一区| 亚洲免费中文字幕| 亚洲一区二区在| 欧美一区二区三区在线观看| 亚洲国产婷婷| 亚洲每日更新| 性色av一区二区怡红| 午夜精品久久久久久久蜜桃app | 欧美日韩精选| 国产精品视频一区二区高潮| 欧美不卡激情三级在线观看| 久久久久久久久综合| 男人的天堂亚洲在线| 另类av一区二区| 欧美aaa级| 欧美日韩国产三区| 国产一区二区激情| 伊人春色精品| 亚洲少妇在线| 亚洲午夜一级| 久久综合给合| 日韩一区二区精品葵司在线| 亚洲欧美区自拍先锋| 老司机久久99久久精品播放免费| 欧美风情在线| 国产精品乱码一区二区三区| 激情丁香综合| 一区二区久久久久久| 久久久国产视频91| 亚洲日本视频| 欧美在线网址| 欧美一区二区三区四区视频| 欧美激情一区二区三区在线视频| 国产精品乱子久久久久| 在线精品国精品国产尤物884a| 亚洲一级免费视频| 久久性天堂网| 亚洲一级二级在线| 女人天堂亚洲aⅴ在线观看| 国产精品三上| 亚洲一二三四区| 欧美韩日视频| 久久国产精彩视频| 国产精品久久一区主播| 91久久极品少妇xxxxⅹ软件|