在MFC对话框应用程序中全天候检查系统时间
Check system time in MFC Dialog Application 24/7
我正试图让我的程序在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
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- 检查程序是手动调用还是通过系统启动调用
- 将glteximage2d保存到文件系统中进行检查
- 在MFC对话框应用程序中全天候检查系统时间
- 检查文件的上次写入时间是昨天由系统时间
- 如何在C++中检查用户的系统权限
- 如何检查路径在 boost::文件系统中是否有效
- boost文件系统3路径包含检查
- C++如何检查文件是否在使用-多线程多进程系统
- 如何检查系统::集合::ArrayList是否存在/为空
- 如何使用C/C++以编程方式检查窗口中的系统文件夹
- 如果系统处于活动状态,如何在C++中进行检查
- 如何检查c++源代码中是否有任何系统/shell调用
- linux下如何检查系统时间是否有效
- 检查一个点是否在阴阳(协调系统)c++的好或坏的部分
- 检查type是否被声明为元类型系统(对于SFINAE)
- Cmake:检查系统是否包含目录
- 检查unix系统中是否加载了共享对象