Pergunta

I am trying to make my program check for the system time 24/7 in a loop in a mfc dialog application.

a little background on what i did so far.

My GUI has a few buttons:- start, stop, exit, and a few edit boxes to show values.

It is meant to read into a .txt file at a predetermined location 24/7 at a specified interval time by the user. This could be 5 mins to however long the user wants, but it has to be in multiples of 5. For example, 5 min, 10 min, 15 min, 20 min so on and so forth.

After reading the .txt file, it will then compare the strings within the .txt file and output to a .csv file.

Thats the brief explanation on what I am trying to do. Now on to the question at hand.

Since I need the program to run 24/7, I am trying to make the program check the system time consistently and trigger a set of functions when the interval time specified by the user has been reached.

For that, I made a variable whenever the start button is pressed

BOOL start_flag = true;

and the start_flag will only return to false once the stop button is pressed

and then I had it in a while loop

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 function//////////

{
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);

}

Right, the problem i have so far is that because of the while loop, the program is stuck in an infinite loop and the GUI freezes due to that and the user wont be able to make it stop.

There are a few things i thought in my head but am not sure if it will work.

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

Any advice would be much appreciated. I hope this post wasnt too confusing in its layout as I wanted to provide as much as I can to show that I am not just asking questions without trying to fix it myself first.

Foi útil?

Solução

Whilst you are in your while loop, the windows message pump is not processing, ergo - your user interface freezes up. You have 2 options, as I see it:

1) Use a background thread to do this.

2) Investigate CWnd::SetTimer and use that to perform the timings. This will post a message in to the message queue at the intervals you specify (It's not a real-time solution, but I don't think you have that requirement), and therefore your interface will remain alive.

Outras dicas

Add timer control in your dialog and handle WM_TIMER message.

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top