• <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>

            Leo

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            • 隨筆 - 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 閱讀(1640) 評論(0)  編輯 收藏 引用

            久久精品国产亚洲AV大全| 久久精品夜色噜噜亚洲A∨| 色偷偷88欧美精品久久久 | 伊人久久大香线蕉精品| 久久综合给合久久国产免费| 久久精品国产久精国产果冻传媒| 久久综合视频网站| 亚洲国产日韩欧美综合久久| 亚洲中文字幕伊人久久无码 | 热RE99久久精品国产66热| 国产激情久久久久影院老熟女| 国产亚洲美女精品久久久| 精品国产综合区久久久久久| 国内精品久久久久久久久| 欧美日韩精品久久久久| 久久久久久久久久久久久久| 久久精品国产99国产精品导航 | 久久狠狠高潮亚洲精品| 亚洲αv久久久噜噜噜噜噜| 久久亚洲精品无码AV红樱桃| 久久91精品国产91久久户| 久久精品成人一区二区三区| 亚洲欧美日韩久久精品| 久久久女人与动物群交毛片 | 久久精品国产91久久综合麻豆自制| 久久国产成人精品麻豆| 久久伊人色| 天天爽天天狠久久久综合麻豆| 国产成人精品久久一区二区三区 | 久久综合五月丁香久久激情| 国产成年无码久久久免费| 久久国产精品99精品国产987| 久久亚洲精品无码观看不卡| 99久久精品国产一区二区| 国产精品一久久香蕉国产线看| 精品久久人人爽天天玩人人妻| 2021国内久久精品| 2020最新久久久视精品爱| 性欧美大战久久久久久久久| 97精品国产97久久久久久免费| 久久久无码精品亚洲日韩京东传媒 |