1

我试图让我的程序在 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
}

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

{
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
}
}

任何建议将不胜感激。我希望这篇文章的布局不会太混乱,因为我想提供尽可能多的信息来表明我不仅仅是在问问题而没有尝试自己先解决它。

4

2 回答 2

0

当你在你的while循环中时,windows消息泵没有处理,ergo - 你的用户界面冻结了。如我所见,您有两个选择:

1)使用后台线程来做到这一点。

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

于 2014-01-08T10:18:54.903 回答
0

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

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

于 2014-01-08T10:21:09.157 回答