[英]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. 我有我的C ++项目,我希望在加载表单40秒后启动一个事件。 I know there is a timer in C++, but it only shows intervals.
我知道C ++中有一个计时器,但它只显示间隔。
I'm using Visual Studio 2008. I'm creating a Windows Form Application. 我正在使用Visual Studio 2008.我正在创建一个Windows窗体应用程序。 Do you guys know what function I have to use?
你们知道我必须使用什么功能吗?
使用40秒间隔设置计时器,并在首次触发计时器时将其移除。
Are you talking about the .NET Timer ? 你在谈论.NET 计时器吗? 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
. 编辑 :间隔以毫秒为单位,因此只需将您的秒数乘以1000即40秒,即
40 * 1000 = 40000
。
EDIT2 : As per your other comment, at the bottom of the link I provided is an example for C++. EDIT2 :根据您的其他评论,我提供的链接底部是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). TimerEventProcessor
被设置为Main
定时器的EventHandler
,并且在定时器被“提升”时运行(当您向表单添加Timer
控件时,Visual Studio可能已经为您设置了)。 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." 你也可以简单地让另一个线程调用
sleep
等待40秒,然后启动你的“事件”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.