[英]How do I create a timer run my program repeatedly? C++
I want create timer in my program so that I can cause it to rerun every minute and I don't know how to do it in a C++ Application.我想在我的程序中创建计时器,以便我可以让它每分钟重新运行一次,但我不知道如何在 C++ 应用程序中执行此操作。 In C# I could just create a timer but I'm struggling here now...在 C# 我可以创建一个计时器,但我现在在这里挣扎......
sleep();睡觉(); is not an option because as far as I know it makes your program inactive for X seconds, I need my app to be active and working, calculating all the time.不是一个选项,因为据我所知,它会使您的程序在 X 秒内处于非活动状态,我需要我的应用程序处于活动状态并正常工作,并且一直在计算。 This is because my code is used to constantly input information into a MS Access table.这是因为我的代码用于不断地将信息输入到 MS Access 表中。 I was able to create the necessary components of my code to connect and perform the insert/update to the table but this is just on of the many components to the code that I am creating.我能够创建我的代码的必要组件来连接并执行对表的插入/更新,但这只是我正在创建的代码的许多组件中的一个。 Please help me with this little (or big?) problem, I'm very new to C++ and learning ATM, but I am developing a fast learning curve.请帮我解决这个小(或大?)问题,我对 C++ 和学习 ATM 很陌生,但我正在开发一个快速的学习曲线。 Thanks谢谢
Every platform provides api for creating a timer, which will give you a callback usually after timer expires.每个平台都提供了 api 用于创建定时器,通常会在定时器到期后给你一个回调。 You can just search for the api available on your platform.您只需搜索平台上可用的 api。
If you are using windows use setTimer function.如果您使用的是 windows,请使用setTimer function。
I suppose you work on Windows, since you mentioned C#.我想你在 Windows 上工作,因为你提到了 C#。 So take a look at SetTimer , and if it is a MFC app, then look at CWnd::SetTimer .所以看看SetTimer ,如果它是 MFC 应用程序,那么看看CWnd::SetTimer 。
If you're using C++ .NET, you can use the same Timer class(es) as C#, just use the C++ syntax (using gcnew instead of new, use the ^ for GC references). If you're using C++ .NET, you can use the same Timer class(es) as C#, just use the C++ syntax (using gcnew instead of new, use the ^ for GC references).
Otherwise you could just have a loop:否则你可能会有一个循环:
while (should_keep_looping) {
// do what you need to do
// if necessary:
sleep(1);
}
See here: http://www.cplusplus.com/forum/beginner/317/见这里: http://www.cplusplus.com/forum/beginner/317/
There is on built in "timer" in C++, and you are correct about the behavior of sleep(). C++ 中有内置的“定时器”,你对 sleep() 的行为是正确的。 The above thread describes a custom implementation.上面的线程描述了一个自定义实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.