Solved

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

Posted on 2000-03-11
14
467 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now