简体   繁体   中英

How do I add a timer to my C++ project?

I have my C++ project, and I want an event to start 40 seconds after my form is loaded. I know there is a timer in C++, but it only shows intervals.

I'm using Visual Studio 2008. I'm creating a Windows Form Application. Do you guys know what function I have to use?

使用40秒间隔设置计时器,并在首次触发计时器时将其移除。

Are you talking about the .NET Timer ? Why doesn't it work for you? That link provides examples (assuming that's what you're talking about) of setting it up and then after it's gone off you can just dispose of it so your event doesn't start multiple times.

EDIT : The interval is in milliseconds, so just multiply your seconds amount by 1000. ie for 40 seconds, 40 * 1000 = 40000 .

EDIT2 : As per your other comment, at the bottom of the link I provided is an example for C++. The TimerEventProcessor is set as an EventHandler for the timer in Main and will be run when the the timer is "raised" (Visual Studio might already set that up for you when you add a Timer control to the form). Since you only want the timer to run once and you don't need the other functionality it provides, I would simply choose to use a thread as I previously suggested:

You could also simply have another thread, which calls sleep to wait for 40 seconds, and then starts your "event."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM