From: success-1 on
On Jan 31, 4:43 pm, "Dave" <davechand...(a)gmail.com> wrote:
> 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/library/microsoft.win32.systemevents...
>
> Any suggestions?
>
> PS I am NOT using dot net


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.




From: success-1 on
On Jan 31, 7:02 pm, succes...(a)myway.com wrote:
> On Jan 31, 4:43 pm, "Dave" <davechand...(a)gmail.com> wrote:
>
> > 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/library/microsoft.win32.systemevents...
>
> > Any suggestions?
>
> > PS I am NOT using dot net
>
> 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.


"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. "

From: Scott McPhillips [MVP] on
Dave wrote:
> 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?

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]

From: Sten Westerback (MVP SDK) on

"JD" <MrJackDanielsBlack(a)Hotmail.com> wrote in message
news:45c13a5f$0$80122$742ec2ed(a)news.sonic.net...
> "Dave" <davechandler(a)gmail.com> wrote in message
> news:1170283433.753622.132120(a)h3g2000cwc.googlegroups.com...
>> 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/library/microsoft.win32.systemevents.createtimer(VS.80).aspx
>>
>> Any suggestions?
>>
>> PS I am NOT using dot net
>>
> 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>
> }

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


From: James Brown on

<success-1(a)myway.com> wrote in message
news:1170291976.926749.80140(a)k78g2000cwa.googlegroups.com...
> On Jan 31, 7:02 pm, succes...(a)myway.com wrote:
>> On Jan 31, 4:43 pm, "Dave" <davechand...(a)gmail.com> wrote:
>>
>> > 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/library/microsoft.win32.systemevents...
>>
>> > Any suggestions?
>>
>> > PS I am NOT using dot net
>>
>> 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.
>
>
> "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. "
>

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