Log in

View Full Version : Timer control, dont see one... Need to trip function every X miliseconds... How?


PPCdev
02-17-2003, 03:21 AM
In VB (Sorry, Im growing up.. :-) there is a nifty Timer control I can drop on my form and set its interval in ms, enable or disable it. In that control I can put code, or a function call, etc..

I need to do the same thing with eVC++. However, I dont see a Timer control.

Im seeing this in the Help:

Timer Control
This control runs code at regular intervals by initiating a Timer event. The Timer control, invisible to the user, is useful for background processing.

Timer
The Timer control supports the following properties.

Enabled Name
Interval Parent
Left Top


However, Im unable to loacte the Timer control on the controls panel, and I dont see how to import it.

Im still new at this... I just got my 2ed SuperNewbie program working, gives percent of battery life. I want to get battery life every X ms and give a message box when the user defined percentage is met (IE: aleart when it drops to 30%).

Thanks for your help folks! Im getting there, with a lot of hacking time Ive started asking a little more educated questions. :-)

Andrew

smashcasi
02-17-2003, 06:06 AM
The best way to do this is generally in a separate thread, but since that opens the door to a whole new level of complication we'll stick with the timer function offered by Windows:

Go to View->Classwizard. In the "Class Name" dropdown select the class associated with the dialog or frame you want the timer to exist in. Next, in the "Messages" list box, scroll down to "WM_TIMER" and double-click on it. Then you'll need to write some code that looks something like this:


// id for the timer event
// (the value doesn't matter so long as its unique among your program's timer events)
#define MY_TIMER_EVENT (10)

// interval at which the timer event occurs (100ms)
#define MY_TIMER_EVENT_INTERVAL (100)

BOOL CMyDlg::OnInitDialog()
{
// ...

SetTimer(MY_TIMER_EVENT, MY_TIMER_EVENT_INTERVAL, NULL);
}

// the OnTimer() function will be generated by the ClassWizard,
// you don't need to create it yourself

void CMyDlg::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case MY_TIMER_EVENT:
// code to execute goes here
break;
}

// ...
}


The above will excute the code for MY_TIMER_EVENT every 100ms. To add additional events just define the new event, call SetTimer() and add a new case handler in the switch statement. That should do it.

If you haven't checked it out already I highly recommend www.pocketpcdn.com. They've got a lot of helpful articles and the search function they have set up hits all of the various PPC development newsgroups. A great source for solutions to problems both large and small.

PPCdev
02-17-2003, 08:39 AM
Many thanks Smash, that got the timer working, I can verify that with MessageBeep(-1);

However, this opens new problems...

I just tried to define my own function. I want to call it on the window initilize event, and in the timer trip. However, I cant call the function, get the messages:

error C2065: 'UpdateBattStatus' : undeclared identifier
error C2365: 'UpdateBattStatus' : redefinition; previous definition was a 'data variable'

Heres the call:

UpdateBattStatus();


And, heres the function:

=============== START FUNCTION ===================

void UpdateBattStatus()
{
//This is my function to display status

SYSTEM_POWER_STATUS_EX2 sps;

CString Rval;

// All this below operates fine when in the window initilization
// even, I just copy and pasted to my function.
memset(&sps,0,sizeof(&sps));
GetSystemPowerStatusEx2(&sps,sizeof(&sps), TRUE);
Rval.Format(_T("%d"), sps.BatteryLifePercent);
Rval = "Battery Percent: " + Rval;


}

=============== END FUNCTION =====================



Now, Im not done.... What used to work in other functions of
the program, do *not* work in *my* function


// This does not work, wanting to set the static text of
// IDC_STATUS on dialog CTest08Dlg
SetDlgItemText(IDC_STATUS, Rval);


// This is weird, its a copy and paste, the exact same thing
// works fine else where, but not in my function. Im wondering
// if I even defined a function to start with! :-)
MessageBox(Rval, _T("Battery Status..."), MB_ICONINFORMATION | MB_OK);



Any advice? I am *slowly* learning as I do, things are coming together, but Im still missing about 900 pieces of the 1,000 piece puzzle so the picture is kinda fuzzy still. :-)

Thanks-
Andrew

smashcasi
02-17-2003, 04:58 PM
Ok, I'll help you out with this one but if you're serious about learning the language I strongly suggest you get ahold of a beginning C++ book and spend some time with it. Or just search around the net for some good tutorials on the basics, it'll help give you a much clearer picture of how all this works.

Assuming that "UpdateBattStatus" isn't defined anywhere else in your program, here's what you'd need to do to fix it:

First, you'll probably notice that every .cpp file has an accompanying .h file. This is called the header file and it's where all of your functions are defined. In most cases, the .cpp file is where they are implemented. The complier error you're getting is telling you that your function probably isn't defined anywhere in the program. To define your function, you'll need to create an entry in the dialog's header file like this:


class CMyDlg : public CDialog
{
// ...

// this is where you insert your code
void UpdateBattStatus();
};


Then in the .cpp file you'll need to add "CMyDlg::" (or whatever your dialog's class name is called) in front of "UpdateBattStatus()". That should take care of the problem.

EvilOne
02-17-2003, 05:17 PM
Not only a C/C++ book, but also a book on MFC, even for the desktop, it will help you understand why things do what they do and why things get called when they do.

PPCdev
02-17-2003, 09:04 PM
Danka, Danka! Muchass Grassyass! :-)

Thats more clear guys, the book didnt specify those requirments, at least none that I read.

This is the book I currently have:
http://images.amazon.com/images/P/0735615675.01.TZZZZZZZ.jpg





I just got these books off Amazon:


Programming Microsoft Windows Ce
http://images.amazon.com/images/P/0735614431.01.MZZZZZZZ.jpg


Windows CE 3.0 Application Programming
http://images.amazon.com/images/P/0130255920.01.MZZZZZZZ.jpg


I hope these two will get here tomorrow! HAHA! Well, they wont, but Ill be awefully glad to see them arrive!

Thanks for your pointer on where to define, how to define. Ill hack on that later this evening.

Andrew