- Timers in Console Applications
- Posted by Dave on January 31st, 2007
I'm having trouble figuring out how to create and use a timer in
Windows when you don't want to build an actual windowed application,
but rather are making a console app. All examples I've found so tie
timers directly into the windows.
For Example:
http://msdn2.microsoft.com/en-us/lib...(VS.80).as px
Any suggestions?
PS I am NOT using dot net
- Posted by Bertel Brander on January 31st, 2007
Dave skrev:
You can use normal windows timers in consols apps:
#include <windows.h>
#include <iostream>
int main()
{
SetTimer(0, 1024, 1000, 0);
SetTimer(0, 1025, 750, 0);
MSG Message;
while(GetMessage(&Message, 0, 0, 0))
{
if(Message.message == WM_TIMER)
{
std::cout << "Timer" << std::endl;
}
}
}
But you need to have a message loop, as above.
I we know more of what you want to do, we could probably give
you better advice.
--
Just another homepage:
http://damb.dk
But it's mine - Bertel
- Posted by Dave on January 31st, 2007
Certainly, thanks for the help!
I have an application that itself owns numerous classes. I'm
implementing a state pattern, where each class is a state. These
state objects are owned in turn by a state manager, who is
instantiated by main.
Some events cause timers to start. When some timers expire they cause
state transitions. Sometimes I will need to stop the timers early.
Other times I'll need to 'tick' a value and reschedule them.
Is there any way I can simply specify a function handler to be called
when the timer expires? I really don't want to have to have my main
handle all possible timer events as they are elements of the states
and not main.
here http://msdn2.microsoft.com/en-us/library/ms644901.aspx they have
an example that doesn't need the GetMessage loop (very last example)
though my attempts to recreate it have failed and I believe it's
because I am not registering a window in the SetTimer command.
another question: How do I stop a timer? Would that just be
'KillTimer'? How about rescheduling the timer? Is that just another
SetTimer call?
On Jan 31, 6:36 pm, Bertel Brander <ber...@post4.tele.dk> wrote:
- Posted by JD on January 31st, 2007
One technique I've used is to run an imitation timer thread that does a
waitforsingleobject with a timeout.
Sort of like: (initialization and errors ignored)
HANDLE eventTimer;
while (true)
{
WaitForSingleObject(eventTimer, 250);
<timer has fired>
}
Mike
"Dave" <davechandler@gmail.com> wrote in message
news:1170283433.753622.132120@h3g2000cwc.googlegro ups.com...
- Posted by Grzegorz Wróbel on February 1st, 2007
Dave wrote:
I think you need SetWaitableTimer()/CancelWaitableTimer().
--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D
- Posted by success-1@myway.com on February 1st, 2007
On Jan 31, 4:43 pm, "Dave" <davechand...@gmail.com> wrote:
UINT SetTimer(
HWND hwnd, // handle of window for timer messages
UINT idTimer, // timer identifier
UINT uTimeout, // time-out value
TIMERPROC tmprc // address of timer procedure
);
"tmprc Points to the function to be notified when the time-out value
elapses. For more information about the function, see the TimerProc
callback function.
- Posted by success-1@myway.com on February 1st, 2007
On Jan 31, 7:02 pm, succes...@myway.com wrote:
"hwnd Identifies the window to be associated with the timer. If this
parameter is NULL, no window is associated with the timer and the
idTimer parameter is ignored. "
- Posted by Scott McPhillips [MVP] on February 1st, 2007
Dave wrote:
You cannot use SetTimer/KillTimer without a message pump.
The key thing you have to figure out is how your program is going to
sense and react to the timer(s). If you are running other code all the
time then it is unrealistic to think execution will just magically
transfer to somewhere else. You probably need to organize the program
control so that it is one giant loop that runs every N milliseconds,
decrements your active timer variables and executes something accordingly.
With that settled there are any number of non-message timers available:
WaitForMultipleObjects, CreateWaitableTimer, timeSetEvent, Sleep, ...
--
Scott McPhillips [VC++ MVP]
- Posted by Sten Westerback \(MVP SDK\) on February 1st, 2007
"JD" <MrJackDanielsBlack@Hotmail.com> wrote in message
news:45c13a5f$0$80122$742ec2ed@news.sonic.net...
Delayed busylooping certain is better than nondelayed.. But i think the OP
wanted to be able to interrupt and reschedule.
By switching to WaitForMultipleObjects(), with one handle being to an "need
to reshedule" event and the other for a WaitableTimer, will allow another
thread to break the wait, figure out when it needs to wake up again and
reconfigure the waitable timer to that time. Then Windows will wake your
thread up only when actually needed.
- Sten
- Posted by James Brown on February 1st, 2007
<success-1@myway.com> wrote in message
news:1170291976.926749.80140@k78g2000cwa.googlegro ups.com...
SetTimer *always* causes a message to be posted to the thread's
message-queue even if no window is present. Which causes problems in console
applications, hence the reason for the original post.
--
James Brown
Microsoft MVP - Windows SDK
www.catch22.net
Free Win32 Tutorials and Sourcecode
- Posted by Bertel Brander on February 1st, 2007
Dave skrev:
You could do:
#include <windows.h>
#include <iostream>
class TimerBase
{
public:
virtual void OnTimer() = 0;
};
class X : public TimerBase
{
public:
void OnTimer()
{
std::cout << "X::OnTimer" << std::endl;
}
};
class Y : public TimerBase
{
public:
void OnTimer()
{
std::cout << "Y::OnTimer" << std::endl;
}
};
int main()
{
X x;
Y y;
SetTimer(0, 1024, 1000, (TIMERPROC )&x);
SetTimer(0, 1024, 750, (TIMERPROC )&y);
MSG Message;
while(GetMessage(&Message, 0, 0, 0))
{
if(Message.message == WM_TIMER)
{
TimerBase* ptr = (TimerBase *)Message.lParam;
ptr->OnTimer();
}
}
}
You could spawn a seperate thread to run the message loop,
but that would requere synchronization between the threads.
--
Just another homepage:
http://damb.dk
But it's mine - Bertel
- Posted by PJ Naughter on February 1st, 2007
On 31 Jan 2007 14:43:53 -0800, "Dave" <davechandler@gmail.com> wrote:
Unless you are running a message pump loop in your console application
i.e. GetMessage / Translate / Dispatchmessage which is not too common,
you would probably be best to use waitable timers in conjuction with a
WaitForMultipleObjects call. If you are interested there is a MFC
wrapper for waitable timers on my web site at
http://www.naughter.com/waitabletimer.html.
Regards,
PJ Naughter
Naughter Software
Mail: pjna at naughter.com
Web: www.naughter.com
Hotmail/MSN Messenger: pjnaughter at hotmail.com
It is finally on the shelves... the book from Joseph Gama and I:
http://www.amazon.com/Super-SQL-Serv.../dp/0976157322