-
January 4th, 2006, 06:47 PM
#1
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
-
January 4th, 2006, 06:52 PM
#2
Re: Timer issue
I am confused
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.
-
January 4th, 2006, 07:24 PM
#3
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.
-
January 4th, 2006, 07:34 PM
#4
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 ?
-
January 4th, 2006, 08:02 PM
#5
Re: Timer issue
The first and most obvious thing to do is debug your Assertion and find out what the problem is.
-
January 4th, 2006, 08:35 PM
#6
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.
-
January 4th, 2006, 08:55 PM
#7
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?
-
January 4th, 2006, 09:00 PM
#8
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.
-
January 4th, 2006, 09:10 PM
#9
Re: Timer issue
classs CMotor,it's not call DoModal(),so it's not have a window..
-
January 4th, 2006, 09:22 PM
#10
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?
-
January 4th, 2006, 09:25 PM
#11
Re: Timer issue
would you want to use CMotor instead of the Base dialog class
-
January 4th, 2006, 10:06 PM
#12
Re: Timer issue
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(); -
-
January 4th, 2006, 11:44 PM
#13
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.
-
January 5th, 2006, 08:25 AM
#14
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?
-
January 5th, 2006, 08:54 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|