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

Leo

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

統(tǒng)計

  • 隨筆 - 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)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導航: 博客園   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>
            亚洲女性喷水在线观看一区| 亚洲午夜一区二区三区| 久久gogo国模裸体人体| 国产欧美日韩综合一区在线播放 | 国产日产欧产精品推荐色| 亚洲综合精品一区二区| 亚洲欧美激情一区二区| 国色天香一区二区| 亚洲电影免费观看高清完整版| 久久精品一本| 日韩西西人体444www| 亚洲私人影院| 韩日欧美一区二区三区| 欧美激情一级片一区二区| 欧美美女操人视频| 欧美一区二区黄色| 老色批av在线精品| 亚洲午夜日本在线观看| 欧美亚洲午夜视频在线观看| 亚洲国产精品久久人人爱蜜臀 | 欧美影片第一页| 久久婷婷麻豆| 亚洲尤物视频网| 久久婷婷蜜乳一本欲蜜臀| 一区电影在线观看| 欧美在线1区| 在线亚洲观看| 久久久久久网| 亚洲欧美日韩在线一区| 另类专区欧美制服同性| 亚洲欧美国产另类| 欧美.com| 久久综合狠狠综合久久激情| 欧美三级视频在线播放| 久久久久综合网| 欧美另类高清视频在线| 久久免费高清视频| 欧美午夜三级| 91久久在线播放| 永久免费精品影视网站| 亚洲欧美激情诱惑| 一区二区三区欧美亚洲| 久久综合一区二区| 久久成人久久爱| 欧美午夜免费电影| 亚洲国产一区二区三区高清| 国内精品视频一区| 亚洲欧美制服另类日韩| 亚洲视频你懂的| 欧美久久久久久蜜桃| 欧美刺激性大交免费视频| 国产一区二区福利| 一区二区欧美日韩视频| 99在线精品免费视频九九视| 久久综合九色综合久99| 久久综合久久美利坚合众国| 国产午夜精品久久久久久免费视 | 欧美日韩一区精品| 亚洲国产婷婷香蕉久久久久久99 | 日韩午夜免费视频| 麻豆91精品91久久久的内涵| 免费高清在线一区| 在线观看一区欧美| 久久米奇亚洲| 欧美国产综合视频| 亚洲激情综合| 欧美精品一区二区三区视频| 亚洲日本一区二区| 日韩视频国产视频| 欧美日韩性生活视频| 日韩视频亚洲视频| 亚洲一区精品在线| 国产精品一区二区三区成人| 亚洲欧美日韩在线综合| 欧美在线一级视频| 激情小说另类小说亚洲欧美| 久久激情视频久久| 欧美激情黄色片| 一本久久知道综合久久| 欧美日韩一区二区三区在线看 | 最新成人av在线| 99国产精品| 国产精品丝袜xxxxxxx| 午夜视频一区| 欧美高清视频www夜色资源网| 亚洲毛片播放| 国产精品自拍在线| 久久乐国产精品| 日韩视频第一页| 欧美在线播放| 91久久亚洲| 国产精品看片资源| 久久精品视频网| 亚洲精品你懂的| 久久精品亚洲热| 99热在这里有精品免费| 国产精品视频精品视频| 久久人人爽爽爽人久久久| 亚洲日本免费| 久久久欧美精品| 一本久久a久久免费精品不卡| 国产亚洲精品一区二区| 麻豆乱码国产一区二区三区| 中文国产成人精品久久一| 男女精品视频| 亚洲自拍另类| 亚洲人成在线播放网站岛国| 国产欧美一区二区精品秋霞影院 | 美女福利精品视频| 亚洲综合视频在线| 亚洲国产另类久久久精品极度| 亚洲欧美国产高清| 亚洲人成绝费网站色www| 国产精品一区二区在线| 欧美精品在线观看播放| 久久国产主播精品| 亚洲一区二区动漫| 亚洲精品日韩在线观看| 久久久久99| 亚洲欧美日韩国产综合| 亚洲精品国产精品国自产观看浪潮 | 91久久在线观看| 裸体一区二区| 久久精品国产亚洲5555| 亚洲一级黄色| 99国产精品久久久| 亚洲福利视频专区| 国产日韩专区| 国产精品亚洲精品| 国产精品s色| 欧美日韩国产在线播放| 欧美成人免费在线| 亚洲综合色丁香婷婷六月图片| 亚洲精品社区| 亚洲激情婷婷| 亚洲黄色成人| 一区在线观看视频| 有码中文亚洲精品| 好吊色欧美一区二区三区四区 | 国产精品久久久久久久7电影| 欧美激情一区二区三区在线视频观看 | 欧美高清在线视频| 欧美电影在线观看| 欧美xx69| 亚洲国产精品成人综合| 亚洲国产日日夜夜| 亚洲国产精品www| 亚洲激情在线播放| 日韩午夜精品| 一本久久精品一区二区| 这里只有视频精品| 亚洲欧美日韩国产成人精品影院| 亚洲在线不卡| 欧美一区二区三区视频在线 | 欧美亚洲色图校园春色| 性欧美大战久久久久久久久| 欧美一区二区三区免费观看| 欧美综合77777色婷婷| 久久亚洲精品视频| 欧美精品成人在线| 国产精品女同互慰在线看| 国产偷国产偷亚洲高清97cao| 国产一区二区三区在线免费观看 | 欧美性猛片xxxx免费看久爱| 国产精品成人在线观看| 国产婷婷色综合av蜜臀av| 激情欧美一区| 亚洲精品国产精品国自产观看浪潮 | 宅男在线国产精品| 欧美一区二区黄| 欧美激情在线有限公司| 99精品国产在热久久下载| 亚洲欧美中文日韩v在线观看| 久久久久九九视频| 欧美日本韩国一区二区三区| 国产伦精品免费视频| 尤物yw午夜国产精品视频| 亚洲乱码视频| 久久国产夜色精品鲁鲁99| 欧美黄污视频| 午夜综合激情| 欧美日产在线观看| 韩国三级在线一区| 一区二区三区视频在线| 久久久久久亚洲精品中文字幕 | 亚洲欧美日韩视频二区| 另类成人小视频在线| 日韩一区二区免费高清| 久久色在线播放| 国产精品久久久久久久久久ktv| 在线免费观看视频一区| 亚洲欧美中文字幕| 亚洲激情黄色| 久久久久在线| 国产亚洲第一区| 亚洲综合国产激情另类一区| 欧美国产日韩一区| 欧美在线在线| 国产免费成人在线视频| 亚洲午夜久久久久久尤物|