CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Timer issue

  1. #1
    Join Date
    Dec 2005
    Posts
    21

    Timer issue

    I have a simple dialog test application written as an MFC app under Visual Studio .Net.

    I have a simple class, with a ON_WM_TIMER() and the associated OnTimer() function. I also have a public function in this class which is called from the base dialog and which has a SetTimer() call.

    When the SetTimer() is executed I get a debug assertion failed. I have tried many different permutations of this with the same result.

    If I create the same timer functionality in the base dialog class and call SetTimer, it works just fine.

    Can any one help?

    Thanks

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Timer issue

    I am confused
    Quote Originally Posted by phild
    I have a simple class, with a ON_WM_TIMER() and the associated OnTimer() function. I also have a public function in this class which is called from the base dialog and which has a SetTimer() call.
    Why is the SetTimer call in a dialog, but the handler in a different class ? How are the 2 classes related.. is one a derived class of another ?
    If I create the same timer functionality in the base dialog class and call SetTimer, it works just fine.
    This contradicts your earlier statement which says you have debug assertion when the SetTimer is called from base dialog.

  3. #3
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    Sorry, perhaps I was not too clear. The handler and the SetTimer call are in the same class called CMotor. The SetTimer call is in a public function called EnableMotor belonging to CMotor. The dialog class instantiates the CMotor class and then calls EnableMotor. EnableMotor does the SetTimer and I get the Assertation.

  4. #4
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Timer issue

    Since you are using SetTimer to post WM_TIMER messages, note that , these messages are sent to a window. So, first thing is CMotor has to be derived from CWnd to receive this message and it should've been created too.. i.e it should have a valid HWND before you call SetTimer on it

    Is CMotor CWnd derived ?

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Timer issue

    The first and most obvious thing to do is debug your Assertion and find out what the problem is.

  6. #6
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    Kirants,

    CMotor is derived from CDialog and so should have a valid handle.

    CGDEF,

    When I try to debug, it appears I don't have the source. The assertion occurs in afxwin2.inl line 182.

  7. #7
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    { ASSERT(::IsWindow(m_hWnd)); return ::SetTimer(m_hWnd, nIDEvent, nElapse,
    lpfnTimer); }

    ok, that is line 182.


    So, it does appear that I don't have a valid widow handle, but why.

    I guess there is something I don't understand.

    I create a new class: class CMotor : public CDialog

    I instantiate the class: CMotor motor

    I call the Enable Motor function: motor.EnableMotor(2)

    enable motor calls SetTimer, but the hWnd is invalid?

  8. #8
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    This is the CMotor.h

    Code:
    #pragma once
    
    
    // CMotor dialog
    
    class CMotor : public CDialog
    {
    	DECLARE_DYNAMIC(CMotor)
    
    public:
    	CMotor(CWnd* pParent = NULL);   // standard constructor
    	virtual ~CMotor();
    
    // Dialog Data
    	enum { IDD = IDD_MOTOR };
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    	DECLARE_MESSAGE_MAP()
    public:
    	afx_msg void OnTimer(UINT nIDEvent);
    	void EnableMotor(int Freq);
    };
    Last edited by phild; January 5th, 2006 at 10:58 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    China-G.D-S.T
    Posts
    19

    Re: Timer issue

    classs CMotor,it's not call DoModal(),so it's not have a window..

  10. #10
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    ok, its not a window

    It is an object.

    is there a way to cause it to have a window handle, or a way to allow it to receive WM_TIMER messages?

  11. #11
    Join Date
    Jan 2006
    Location
    China-G.D-S.T
    Posts
    19

    Re: Timer issue

    would you want to use CMotor instead of the Base dialog class

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Timer issue

    Quote Originally Posted by phild
    ok, its not a window

    It is an object.

    is there a way to cause it to have a window handle, or a way to allow it to receive WM_TIMER messages?
    You'd need to create the window by calling motor.Create(); -

  13. #13
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    Thanks GCDEF

    Ok, at least I no longer get the Debug Assert, however, my onTimer handler never gets executed. It would appear that the messages are not getting into my class???


    CMotor motor;
    motor.Create(IDD_MOTOR, NULL);
    motor.EnableMotor(2);

    This is the code in the dialog to instantiate the class, create the window and call the function which calls SetTimer.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Timer issue

    What does your EnableMotor function look like?

    What does you OnTimer function look like?

    Do you have ON_WM_TIMER in you motors message map?

  15. #15
    Join Date
    Dec 2005
    Posts
    21

    Re: Timer issue

    GCDEF, thanks for your continued help and assistance....

    As you can see in the code below, I have tried both using my own TIMERPROC and using the system provided onTimer, neither of which work.

    This is the header:

    Code:
    #pragma once
    
    
    // CMotor dialog
    
    class CMotor : public CDialog
    {
    	DECLARE_DYNAMIC(CMotor)
    
    public:
    	CMotor(CWnd* pParent = NULL);   // standard constructor
    	virtual ~CMotor();
    
    // Dialog Data
    	enum { IDD = IDD_MOTOR };
    
    protected:
    	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
    	DECLARE_MESSAGE_MAP()
    public:
    	afx_msg void OnTimer(UINT nIDEvent);
    	void EnableMotor(int Freq);
    };
    
    
    
    
    This is the code:
    
    
    // Motor.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Pid.h"
    #include "Motor.h"
    #include ".\motor.h"
    
    
    // CMotor dialog
    
    #define	IDT_MotorTick 1
    
    void CALLBACK EXPORT TimerProc(
       HWND hWnd,      // handle of CWnd that called SetTimer
       UINT nMsg,      // WM_TIMER
       UINT nIDEvent,   // timer identification
       DWORD dwTime    // system time
    );
    
    
    
    
    IMPLEMENT_DYNAMIC(CMotor, CDialog)
    CMotor::CMotor(CWnd* pParent /*=NULL*/)
    	: CDialog(CMotor::IDD, pParent)
    {
    	
    	//Create(IDD_PID_DIALOG, NULL);
    }
    
    CMotor::~CMotor()
    {
    }
    
    void CMotor::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    }
    
    
    BEGIN_MESSAGE_MAP(CMotor, CDialog)
    	ON_WM_TIMER()
    END_MESSAGE_MAP()
    
    
    // CMotor message handlers
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////
    void CALLBACK EXPORT TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
    {
    	int i;
    
    	i = 1;
    
    }
    
    
    
    void CMotor::OnTimer(UINT nIDEvent)
    {
    	// TODO: Add your message handler code here and/or call default
    
    	CDialog::OnTimer(nIDEvent);
    }
    
    
    
    
    void CMotor::EnableMotor(int Freq)
    {
    	unsigned int	uResult;
    	int	f;
    	int Timer_IDT;
    
    	f = Freq;
    	Timer_IDT = IDT_MotorTick;
    
    	//uResult = SetTimer(IDT_MotorTick, Freq, (TIMERPROC) TimerProc);
    	uResult = SetTimer(1, 2, (TIMERPROC) NULL);
    
    }
    Last edited by phild; January 5th, 2006 at 10:59 PM.

Page 1 of 2 12 LastLast

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