我试图让我的程序在 mfc 对话框应用程序中循环检查系统时间 24/7。
关于我到目前为止所做的一些背景。
我的 GUI 有几个按钮:- 开始、停止、退出和一些用于显示值的编辑框。
它意味着在指定的时间间隔内由用户在预定位置 24/7 读入 .txt 文件。这可以是 5 分钟到用户想要的任何时间,但必须是 5 的倍数。例如,5 分钟、10 分钟、15 分钟、20 分钟等等。
读取.txt 文件后,它会比较.txt 文件中的字符串并输出到.csv 文件。
这就是我正在尝试做的事情的简要说明。现在开始手头的问题。
因为我需要程序 24/7 运行,所以我试图让程序始终如一地检查系统时间,并在达到用户指定的间隔时间时触发一组函数。
为此,我在按下开始按钮时创建了一个变量
BOOL start_flag = true;
只有按下停止按钮后,start_flag 才会返回 false
然后我在一个 while 循环中得到它
while (start_flag)
{
Timer(); // To add the user entered interval time to current time
Timer_Secondary(); // To compare the converted time against the current time
Read_Log(); // Read the logs
}
///////////////////Timer function//////////////////////
{
CTime curTime = CTime::GetCurrentTime();
timeString_Hour = curTime.Format("%H");
timeString_Minute = curTime.Format("%M");
timeString_Second = curTime.Format("%S");
Hour = atoi(timeString_Hour);
Minute = atoi(timeString_Minute);
Second = atoi(timeString_Second);
if ((first_run == false) && (Int_Frequency < 60))
{
int Minute_Add = Minute + Int_Frequency;
if (Minute_Add >= 60)
{
Minute_Add = Minute_Add - 60;
Hour = Hour + 1;
}
Minute = Minute_Add;
}
if ((first_run == false) && (Int_Frequency >= 60))
{
int Local_Frequency = Int_Frequency;
while (Local_Frequency >= 60)
{
Local_Frequency = Local_Frequency - 60;
Hour = Hour + 1;
}
}
if (first_run)
{
Hour = Hour + 1;
Minute = 00;
Second = 00;
first_run = false;
}
timeString_Hour.Format("%d", Hour);
timeString_Minute.Format("%d", Minute);
timeString_Second.Format("%d", Second);
////////Timer_Secondary函数//////////
{
CTime curTime = CTime::GetCurrentTime();
timeString_Hour_Secondary = curTime.Format("%H");
timeString_Minute_Secondary = curTime.Format("%M");
timeString_Second_Secondary = curTime.Format("%S");
Hour_Secondary = atoi(timeString_Hour);
Minute_Secondary = atoi(timeString_Minute);
Second_Secondary = atoi(timeString_Second);
是的,到目前为止,我遇到的问题是,由于 while 循环,程序陷入了无限循环,因此 GUI 卡住,用户无法使其停止。
我脑子里想了一些事情,但不确定是否可行。
while (start_flag)
{
if((Hour_Secondary == Hour) && (Minute_Secondary == Minute) && (Second_Secondary == Second))
{
// Run parsing function in this (main bit of code)
start_flag = false; //Set it to false so it will jump back out of this loop
}
if ((Hour_Secondary != Hour) && (Minute_Secondary != Minute) && (Second_Secondary != Second))
{
// Some form of time function in this to wait every 1 min then loop back to start of while loop)
// With the timer function, the GUI should be usable at this point of time
}
}
如有任何建议,我们将不胜感激。我希望这篇文章的布局不会太困惑,因为我想提供尽可能多的内容来表明我不只是在问问题而不先尝试自己修复它。
最佳答案
当您处于 while
循环中时,windows 消息泵未处理,因此 - 您的用户界面卡住。在我看来,您有 2 个选择:
1) 使用后台线程来执行此操作。
2) 调查 CWnd::SetTimer
并用它来执行计时。这将按您指定的时间间隔将消息发布到消息队列(这不是实时解决方案,但我认为您没有该要求),因此您的界面将保持事件状态.
关于c++ - 在 MFC 对话框应用程序中检查系统时间 24/7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20992553/