在MFC对话框应用程序中全天候检查系统时间

Check system time in MFC Dialog Application 24/7

本文关键字:检查 系统 时间 全天候 MFC 对话框 应用程序      更新时间:2023-10-16

我正试图让我的程序在mfc对话框应用程序的循环中全天候检查系统时间。

到目前为止我做了些什么。

我的GUI有几个按钮:-start、stop、exit和一些显示值的编辑框。

它是指由用户在指定的时间间隔内全天候在预定位置读取.txt文件。这可能是5分钟,无论用户想要多长时间,但必须是5的倍数。例如,5分钟、10分钟、15分钟、20分钟等等。

读取.txt文件后,它将比较.txt文件中的字符串并输出到.csv文件。

这就是我想做什么的简要解释。现在来谈谈手头的问题。

由于我需要程序全天候运行,我试图让程序始终检查系统时间,并在达到用户指定的间隔时间时触发一组功能。

为此,每当按下启动按钮时,我都会做一个变量

BOOL start_flag = true;

并且start_flag只有在按下停止按钮后才会返回到false

然后我在一段时间内完成了循环

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
}

///////////////////定时器功能/////////\////////

{
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消息泵没有处理,因此您的用户界面冻结了。在我看来,你有两个选择:

1) 使用背景线程来执行此操作。

2) 调查CWnd::SetTimer并使用它来执行计时。这将按照您指定的时间间隔将消息发布到消息队列中(这不是实时解决方案,但我认为您没有这个要求),因此您的接口将保持活动状态。

在对话框中添加计时器控件并处理WM_timer消息。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901(v=vs.85).aspx#creating_timer