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

Thread: Timer Failing

  1. #1
    Join Date
    Oct 2002
    Posts
    1,134

    Timer Failing

    This one's driving me nuts. I have a FormView app that contains a "Suspend" button. When "Suspend" is pressed, I want the title bar to flash a message, 500 msec on, 500 msec off. Which it does, for a while (several minutes to several hours).

    But then the flashing stops. Indications are that the interval timer isn't firing for several seconds. Has anybody run into this sort of problem before?

    Here's the relevant portion of my OnSuspendButton routine:
    Code:
    for( m_FlashTimer = 1; m_FlashTimer < 100; ++m_FlashTimer )
    {
      if( SetTimer( m_FlashTimer, 500, NULL ) break;
    }
    if( m_FlashTimer >= 100 ) m_FlashTimer = 0;    // No timers available
    And the OnTimer routine:
    Code:
    void CMyView::OnTimer(UINT nIDEvent) 
    {
      if( nIDEvent == m_FlashTimer )
      {
        // DEBUG - Find out if the timer is failing to fire
        static CTime old_time = CTime::GetCurrentTime();
        CTime new_time = CTime::GetCurrentTime();
        if( new_time - old_time > 1 )
        {
          Log.Write( "Timer interrupt is late.", 2 );
        }
        old_time = CTime::GetCurrentTime();
    
        // Flash the title bar for "suspend"
        m_bToggleFlash = !m_bToggleFlash;
        CMainFrame *pFrame = (CMainFrame *)GetParentFrame();
        if( pFrame == NULL )
        {
          Log.Write( "GetParentFrame has failed.", 2 );
        }
        else
        {
          if( pFrame->SendMessage( WM_NCPAINT, 1 ) != 0 )
            Log.Write( "SendMessage( WM_NCPAINT ) has failed.", 2 );
        }
        return;
      }
    And, finally, CMainFrm's OnNcPaint:
    Code:
    void CMainFrame::OnNcPaint() 
    {
      CWindowDC	dc( this );
      CRect		TitleBar;
      int		nTitleHeight;
    
      // Find the size of the title bar
      GetWindowRect( &TitleBar );		// The total window size
    
      // Paint the non-title bar
      HRGN hRgnTitle = ::CreateRectRgnIndirect( &TitleBar );
      DefWindowProc( WM_NCPAINT, (WPARAM)hRgnTitle, NULL );
      DeleteObject( hRgnTitle );
    
      if( ((CMyView *)GetActiveView())->m_bToggleFlash )
      {
        Log.Write( "Display normal title bar", 2 );
        return;
      }
    
      // Paint the title bar
      nTitleHeight = ::GetSystemMetrics( SM_CYSIZE ) + ::GetSystemMetrics( SM_CYFRAME );
      TitleBar = CRect( ::GetSystemMetrics( SM_CXFRAME ), ::GetSystemMetrics( SM_CYFRAME ), TitleBar.right - TitleBar.left - ::GetSystemMetrics( SM_CXFRAME ), nTitleHeight );
    
      CFont Font;
      Font.CreateFont( TitleBar.Height(), 0, 0, 0, FW_BOLD, FALSE, FALSE, NULL, NULL, NULL, NULL, NULL, NULL, "Arial" );
      CFont *pOldFont = dc.SelectObject( &Font );
      if( pOldFont == NULL )
      {
        Log.Write( "SelectObject failed", 2 );
        return;
      }
      CString szText = "OPERATION SUSPENDED";
      dc.SetBkColor( RGB( 255, 255, 0 ) );
      dc.SetTextColor( RGB( 255, 0, 0 ) );
      CPoint pt = dc.GetTextExtent( szText );
      int x = TitleBar.left + ( TitleBar.Width() - pt.x ) / 2;
      int y = TitleBar.top + ( TitleBar.Height() - pt.y ) / 2;
      if( !dc.ExtTextOut( x, y, ETO_OPAQUE, &TitleBar, szText, NULL ) )
      {
        Log.Write( "ExtTextOut failed.", 2 );
      }
      dc.SelectObject( pOldFont );
      return;
    }
    Regards
    Robert Thompson

  2. #2
    Join Date
    May 2005
    Posts
    121

    Re: Timer Failing

    You are definitely doing something wrong. Why are you setting 100 timers in the first bit of code? As far as I can see they will all expire at roughly the same time!

    Markus

  3. #3
    Join Date
    Oct 2002
    Posts
    1,134

    Re: Timer Failing

    I confess to leaving out a parenthesis in that "if" statement, Markus. If you add it in, you'll see that the "break" is taken when a timer is assigned. This is almost always the first time the SetTimer routine is entered.
    Regards
    Robert Thompson

  4. #4
    Join Date
    May 1999
    Posts
    117

    Re: Timer Failing

    Why don't you just create a single timer that is called every 500 milliseconds that sends the change message?

    I see where only one is created and then the break is hit, but why the 100 attempts? Is it just for good measure to make sure a timer is set?
    Last edited by lsvedin; May 26th, 2005 at 05:00 PM.

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Timer Failing

    Quote Originally Posted by TSYS
    This one's driving me nuts. I have a FormView app that contains a "Suspend" button. When "Suspend" is pressed, I want the title bar to flash a message, 500 msec on, 500 msec off. Which it does, for a while (several minutes to several hours).
    Did you consider FlashWindowEx() function?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Oct 2002
    Posts
    1,134

    Re: Timer Failing

    I didn't know about FlashWindowEx, thanks for pointing it out. I had hoped for something more visually striking than having the title bar toggle between "active" and "inactive" colors, though (hence the red text on yellow background), but I'll try FlashWindowEx.
    Regards
    Robert Thompson

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Timer Failing

    Quote Originally Posted by TSYS
    I didn't know about FlashWindowEx, thanks for pointing it out. I had hoped for something more visually striking than having the title bar toggle between "active" and "inactive" colors, though (hence the red text on yellow background), but I'll try FlashWindowEx.
    Wouldn't you have it if you leave your NCPAINT handler as is?
    Just set up a flag that you are in a "flashing" mode, and paint accordingly.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured