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

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| 亚洲综合视频一区| 亚洲午夜伦理| 国内一区二区在线视频观看| 久久视频在线视频| 欧美成人免费视频| 一区二区精品在线观看| 亚洲婷婷国产精品电影人久久| 国产精品久久久久一区| 久久久999精品视频| 久久五月天婷婷| 一区二区欧美亚洲| 亚洲永久字幕| 亚洲国产毛片完整版| 亚洲精品日韩欧美| 国产日韩欧美综合| 亚洲国产91色在线| 欧美三级小说| 免费成人性网站| 欧美视频中文字幕| 久久婷婷国产麻豆91天堂| 欧美风情在线观看| 欧美一区二区三区婷婷月色| 久久亚洲图片| 午夜国产精品影院在线观看| 快射av在线播放一区| 亚洲午夜精品福利| 久久亚洲一区二区| 香蕉成人久久| 欧美区视频在线观看| 久久国产黑丝| 欧美日韩在线看| 免费日本视频一区| 国产精品色午夜在线观看| 亚洲国产成人高清精品| 国产色爱av资源综合区| 亚洲欧洲一区二区三区久久| 国产综合色在线视频区| 一区二区三区黄色| 亚洲激情一区二区| 久久激五月天综合精品| 亚洲免费影视| 欧美日本在线播放| 欧美成人精品一区二区三区| 国产欧美69| 一区二区精品在线| 亚洲理论在线| 欧美www视频| 美女黄色成人网| 国内精品一区二区三区| 亚洲一区二区三区中文字幕| 在线亚洲免费| 欧美—级a级欧美特级ar全黄| 久久综合精品一区| 国产在线观看一区| 亚洲欧美日韩高清| 亚洲欧美国产日韩天堂区| 欧美激情精品久久久六区热门| 免费精品视频| 亚洲国产精品嫩草影院| 亚洲欧美制服另类日韩| 欧美另类综合| 亚洲精品美女在线观看播放| 亚洲人成啪啪网站| 免费高清在线一区| 亚洲福利免费| 一区二区不卡在线视频 午夜欧美不卡在 | 亚洲视频欧洲视频| 欧美日本在线| 中文欧美在线视频| 欧美亚洲在线视频| 国产一区二区三区av电影| 亚洲自拍偷拍麻豆| 久久久久国色av免费看影院| 国内久久精品| 久久在线91| 亚洲国产精品第一区二区三区| 91久久久久| 欧美日韩视频第一区| 一区二区激情| 久久精品视频在线播放| 亚洲大胆视频| 欧美久久在线| 亚洲特黄一级片| 久久激情五月丁香伊人| 亚洲第一页在线| 欧美精品久久久久久久| 亚洲一区二区精品在线| 久久久999成人| 亚洲精品一区中文| 国产精品高清在线| 久久国产免费| 99国产精品久久久| 久久精品成人一区二区三区蜜臀 | 欧美福利电影网| 中国日韩欧美久久久久久久久| 欧美综合第一页| 亚洲国产精品久久人人爱蜜臀| 欧美日韩一区二区三区四区在线观看 | 欧美在线免费观看亚洲| 欧美激情亚洲另类| 亚洲欧美日韩区| 亚洲国产精品毛片| 国产精品劲爆视频| 蜜桃av噜噜一区| 亚洲欧美欧美一区二区三区| 欧美成人综合在线| 性欧美办公室18xxxxhd| 亚洲精品美女免费| 欧美一区二区三区视频在线观看| 欧美jizzhd精品欧美巨大免费| 亚洲视频一区在线| 亚洲丶国产丶欧美一区二区三区 | 欧美一区视频在线| 亚洲乱码精品一二三四区日韩在线| 久久本道综合色狠狠五月| 亚洲麻豆一区| 亚洲大片在线观看| 国产在线欧美日韩| 国产精品爱久久久久久久| 欧美a级片网站| 久久久久久久综合狠狠综合| 中文精品99久久国产香蕉| 欧美国产一区二区在线观看| 久久久国产精品亚洲一区| 亚洲在线不卡| 一区二区欧美在线| 亚洲免费高清| 亚洲精选在线观看| 亚洲国产综合视频在线观看 | 欧美成人一区二免费视频软件| 午夜在线成人av| 亚洲一级电影| 亚洲一级一区| 亚洲一品av免费观看| 一区二区三区视频在线播放| 亚洲精品小视频| 亚洲国产精品第一区二区三区| 美女精品在线| 牛牛国产精品| 欧美电影在线观看完整版| 欧美v亚洲v综合ⅴ国产v| 欧美jizz19性欧美| 免费看黄裸体一级大秀欧美| 久久综合狠狠综合久久综合88 | 在线视频日韩| 亚洲一区二区三区视频| 亚洲午夜伦理| 欧美一区二区黄| 久久精品理论片| 久久久www成人免费无遮挡大片| 久久国产福利| 久久亚洲国产成人| 欧美成人一区二区三区在线观看 | 欧美成人精品一区| 亚洲成人在线视频播放| 亚洲最新视频在线| 一区二区三区久久精品| 亚洲综合导航| 久久婷婷影院| 亚洲国产精品视频| 一本久久综合亚洲鲁鲁| 亚洲欧美日韩国产中文| 久久精品欧美| 欧美老女人xx| 国产欧美综合一区二区三区| 精品1区2区3区4区| 洋洋av久久久久久久一区| 亚洲影院色无极综合| 久久精品欧美日韩精品| 欧美国产一区二区| 一本一本久久a久久精品牛牛影视| 亚洲欧美日韩国产综合精品二区| 久久国产精品99精品国产| 欧美高清在线观看| 国产精品亚洲一区| 亚洲黄色av一区| 亚洲在线1234| 欧美大秀在线观看 | 久久国产视频网| 亚洲电影第三页| 亚洲欧美国产制服动漫| 免费观看成人网| 国产精一区二区三区| 亚洲国产综合在线看不卡| 亚洲欧美国内爽妇网| 欧美成人久久| 午夜日韩福利| 欧美日韩亚洲免费| 亚洲国产成人tv| 欧美一区亚洲二区| 亚洲精品久久| 久久免费国产| 国产伦精品一区二区三区| 日韩一级视频免费观看在线| 久久―日本道色综合久久| 一本不卡影院| 欧美精品精品一区|