0

I'm trying to make a little alert system to do something (IDK what I want yet) when a certain time arrives. However, I'm getting all these errors when I run that code in a WinForm after some brief changes. But this code (unchanged) worked in my empty project! I have the snippet below:

int checktime ()
{
    SYSTEMTIME time;
    GetLocalTime( &time );
    int hour = time.wHour;
    int minute = time.wMinute;
    if (hour > 12) hour -= 12;
    if hour == 8
    {
        this->progressBar1->PerformStep();
    }
}

As always, I would appreciate any help or suggestions available. Thanks!

2 Answers2

2
if hour == 8

That's not valid C++ or C++/CLI. The parentheses in an if statement are not optional.

And why aren't you using .NET System::DateTime::Now? That whole function could be:

int checktime ()
{
   int hour = System::DateTime::Now.Hour;
   if (hour == 8 || hour == 20)
    this->progressBar1->PerformStep();
}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Use a Timer object to keep track of the passing time.

In the designer, drag a timer over from the toolbox onto your form, or instantiate one in the code. You can set the duration (using the interval member), and with each "tick", the system raises an event, and you can perform an action.

It's nice because it's almost like having another thread keeping track in the background.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • Inside the timer tick, you might still need to inspect the current time. This snippet doesn't appear to be waiting for a particular time. – Ben Voigt Jun 12 '11 at 03:06
  • @Ben I agree that a check of the current time would be a good idea, if only for correcting any drift in the interval between the tics. My impression was that the OP was trying for 8am (edit: or 8pm also, as you have pointed out), though the code is not specific enough. – jonsca Jun 12 '11 at 03:10