dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Problem using timers in a thread controlling function

  1. #1
    Join Date
    May 2003
    Posts
    1

    Question Problem using timers in a thread controlling function

    Hi,

    I have a thread function T1. In T1 i have another funtion F1 within a continous while loop. In F1 i create my timer through SetTimer.
    I want to perform certain actions through my timer CALLBACK.

    When i execute the program, the control comes to the thread funtion T1, and then to F1 and the timer is created. But the CALLBACK function (TP)is not getting called, even if i give the delay time as 0. The code is be similar to the following.

    T1
    {
    ....
    ....
    while(TRUE)
    {
    F1();
    }

    }

    F1()
    {
    int TimerId= SetTimer(NULL,NULL,0,TP);
    ....
    }

    VOID CALLBACK TP(HWND hwnd, UINT uMsg, UINT_PTR
    idEvent, DWORD dwTime )
    {
    .....
    .....
    }
    What is the reason? and how can i solve the same? Pls give your views.

    Thanks,
    Suganthi

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    From ::SetTimer in MSDN
    Remarks
    An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.
    So the message has to be processed for the callback to be called, but you are in this while loop so the messages are not pumped.

    CWnd::SetTimer just calls this WIN32 API version.

    You can perhaps pump the message loop yourself in the while loop to get the timer function to get called.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Callback function TP contains quite a few parameter arguments. Is that the same format that SetTimer() accepts?

    Kuphryn

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    CWnd::SetTimer
    UINT SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );


    kuphryn, do you not agree with me? The callback function is called by the default window Proc when it receives an WM_TIMER message, so it want be called when the program is in a while loop.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Good point, soul.

    Where does function T1 reside? Does it reside in a worker thread or main thread?

    You are making this timer callback issue way more than it really is. I recommend a timer queue timer or create a custom timer via worker threads.

    Kuphryn

  6. #6
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917
    Originally posted by souldog
    So the message has to be processed for the callback to be called, but you are in this while loop so the messages are not pumped.
    Even if thread did not have while loop timer procedure would not be called.

    Originally posted by souldog
    The callback function is called by the default window Proc when it receives an WM_TIMER message
    You have to establish message queue (local message loop) inside of the thread. This will be sufficient enough to make timer working by dispatching messages. USER32 will call timer procedure from _UserCallWinProc. So, you were right:

    Originally posted by souldog
    You can perhaps pump the message loop yourself in the while loop to get the timer function to get called.
    The only draw back is that waiting for a message will suspend a thread until something is in the queue; GetMessage does not come back until there is a message to retrieve. Since thread will not receive other but timer message, it will be suspended for a duration of a timer.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    hi, john. Thanks for the clarifications. I was assuming that the thread being used has a message loop, otherwise of course this will not work.

    Also I was thinking of PeekMessage not GetMessage.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  8. #8
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917
    PeekMessage returns immediately, therefore it would tie up application to the point that it would appear frozen. Since message queue is established in the thread also requires OS attention. Try, and see it. It is interesting.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)