Solved

How to re-start a timer in VC++?

Posted on 2000-03-11
14
488 Views
Last Modified: 2010-04-10
I need a timer in a VC++ application. I use the Timer Object (COM) to do this. To my surprise, once the timer is stopped, it cannot be restarted (using timer.SetEnabled(TRUE/FALSE) function).
Somebody tell me how to solve the problem?

Is this Timer Object in VC++ different from the timer in VB resource panel (there is no similar problem in VB application).
0
Comment
Question by:yzmao
  • 6
  • 5
  • 3
14 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2608678
What sort of timer are you using?

You can use SetTimer() for a "windows timer" but this is only suitable for low resolution timing where accuracy is not crucial.

You can use the Multi-mediat timers for high'er resolution timing.  Look a timeSetEvent().
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2608893
->I need a timer in a VC++ application. I use the Timer Object (COM) to do this. To my surprise, once the timer is stopped, it cannot be restarted (using timer
========================
As nietod said,since you are in VC++,you'd better use SetTimer() to set a clock.Or use other high-resolution timer functions.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2608894
check:
timeGetTime()
QueryPerformanceCounter()QueryPerformanceFrequency()
GetTickCount()

Regards
W.Yinan
 
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:yzmao
ID: 2609639
I appreciate very much all your answers. So it is not that good if I use COM timer? -- There is a COM Timer in the resource library called Timer Object. I thought I can use it directly.

I don't need the accuracy -- what the timer needs to do is that, every half a second, it triggers an event to check the URL on the browser. But I need to stop the timer and restart it when necessary. I thought for this the Timer Object is the easiest.

I'll try follow your answer. Give me more instructions if you think it is necessary.

yzmao

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2609672
You can use settimer() if accuracy is not important.
In your winmain(),add SetTimer(hwnd,0,500,0);
And in your window message-procdure,add handler of WM_TIMER/Or OnTiemr() in MFC

bool bwork=TRUE;
case WM_TIMER:
if(bwork)
{
//do whatever you want and change bwork to FALSE to stop the timer
}

Feel free to ask.

Regards
W.Yinan
0
 
LVL 22

Expert Comment

by:nietod
ID: 2609681
You must have a window and a message loop to use a window timer.  Do you?

I think SetTimer had been my suggestion.  :-)
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2609698
->I think SetTimer had been my suggestion.  :-)
===========================
So what ?

I'v withdrawed it...

Anyway , hope code above helpful

Regards
W.Yinan
0
 
LVL 22

Expert Comment

by:nietod
ID: 2609719
Usually, you don't submit an answer that was proposed by another expert in a comment.  It is the EE equivalent of plaugerism.  

For example, on the "last" question where you said the client was making the wrong type of project (it was looking for main()), I submitted a comment, since you had the right answer first.  In that case however, the client accepted my comment as an answer, even though it really was suggested by you first.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2609729
:)
I like you ,nietod.
You are very frank.

Good luck and leading a happy life.

Regards
W.Yinan
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2609734
->Usually, you don't submit an answer that was proposed by another expert in a comment.  It is the EE equivalent of plaugerism.  
=========================
Thanks.
I know this.
In this case I readed that  yzmao said "I'll try follow your answer. Give me more instructions if you think it is necessary."

So ,I think he has already chosen SetTimer() and need more.
Thus I do this.

Regards
W.Yinan
0
 

Author Comment

by:yzmao
ID: 2610118
It seems I caused some trouble. Sorry about that. I appreciate everyone's effort.

For this question, I may use my own method -- not turn off the timer, just set a flag. It works now.

But I will use your solutions sometime later.

Cheers,

yzmao
0
 
LVL 22

Expert Comment

by:nietod
ID: 2610223
>> It seems I caused some trouble.
Not at all.    

wyn's the trouble maker.   :-)    Not really.

>> not turn off the timer, just set a flag.
>> It works now.
I don't understand.

You can stop the windows timer with KillTimer().
0
 

Author Comment

by:yzmao
ID: 2610750
As I said, there is a COM component called Timer Object that we can use in a VC++ application. But once it is turned off (by calling SetEnabled(FALSE), it could not be turned on again -- that is the original question.

See, if I didn't turn it off but use a flag to invoke a certain procedure, then I can get the desired result. It is working now.

Thanks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2611820
I see.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question