简体   繁体   English

让程序在MFC C ++中每小时运行一些函数

[英]Getting a program to run certain functions every hour in MFC C++

I am trying to create a program that runs certain functions every hour in mfc c++. 我试图在mfc c ++中创建一个每小时运行某些功能的程序。

I couldn't find any good tutorial outside on how to do this as I am still in the learning process and trying to expand my knowledge on this. 我在外面找不到关于如何做到这一点的任何好的教程,因为我仍在学习过程中并试图扩展我的知识。

Just a little something so it wouldn't seem that I am just asking empty questions, so far I created a program that reads value from an .INI file and stored it in the global variables within the program. 只是一点点,所以我似乎不会问空问题,到目前为止,我创建了一个从.INI文件中读取值并将其存储在程序中的全局变量中的程序。 I've manage to make it run indefinitely in the System Tray and my plan is to ultimately parse some text from a .txt file into a .csv file every hour. 我已经设法让它在系统托盘中无限期运行,我的计划是最终每小时将.txt文件中的一些文本解析为.csv文件。

So to reiterate, can anyone explain to me the logic and a rough method on how to do it? 重申一下,任何人都可以向我解释如何做到这一点的逻辑和粗略方法吗? For my learning process, i would just like to know how i can show 对于我的学习过程,我只想知道如何展示

MessageBox("Hello");    

every 1 minute or so just for my own understanding. 每1分钟左右只是为了我自己的理解。

As you're using MFC then I'm supposing you're using Windows as your OS. 当你使用MFC我假设你正在使用Windows作为你的操作系统。

The normal way of approaching this is to use the Task Scheduler in Windows to schedule the running of your program at the desired intervals. 接近这一正常的方法是使用任务计划程序在Windows在所需的时间间隔来安排你的程序的运行。 (You can use the Task Scheduler to configure command line arguments too). (您也可以使用任务计划程序配置命令行参数)。

I'd be reluctant to use timers in the application itself as I'd question the stability of such an approach. 我不愿意在应用程序中使用计时器,因为我质疑这种方法的稳定性。

Use a Windows timer. 使用Windows计时器。 Get the wall-time and calculate how many ms are left until the next XX:00. 获取壁垒时间并计算在下一个XX:00之前剩余的ms数。 Set the timer interval to, (say), half that. 将计时器间隔设置为(比方说)一半。 In the timer handler, repeat that calcuation and timer-set until the time remaining is less than 1000 ms and then set the timer to the remaining interval. 在计时器处理程序中,重复该计算和计时器设置,直到剩余时间小于1000毫秒,然后将计时器设置为剩余间隔。 Next time it fires, perform your operation and then set the timer for the next hour. 下次触发时,执行操作,然后设置下一小时的计时器。

You can 'creep up' on the hour quite accurately in such a manner. 你可以通过这种方式非常准确地“爬行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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