D
DaveN
Hi All,
I've just started with Java in the last couple of days and am now having
some trouble finding a clear description of implementing a timer, this
however may be down to my lack of experience with OOP, so I apologise if
that's the case!
I understand the need for the java.util.timer/Task classes to get the
correct things available to me.
I need to have a timer running in a background thread that calls a function
every say 1s. That function will do some things like updating a screen
message and some other application calculations. It also needs to stop the
timer when the necessary conditions are met. The timer will have been
started when the user requests.
My pseudo java code for this is something like:
void MainFunction()
{
if(UserMakesStartSelection == 1)
{
Timer timer = new CreateNewTimer( TimerFunctionToCall, StartNow,
Every1000ms)
}
}
void TimerFunctionToCall()
{
/* Gets called every 1000ms */
/* Do some things */
GetTimeElapsed();
UpdateScreen();
.
.
.
etc
/* Check conditions */
if(TimeToStop == 1)
{
StopTimer(timer);
}
}
All the examples I've seen seem to have these functions all wrapped together
in a class which makes it difficult to understand and follow. Like I say
it's probably my lack of OOP but if anyone can help to clear the muddy
waters I would appreciate it.
I've just started with Java in the last couple of days and am now having
some trouble finding a clear description of implementing a timer, this
however may be down to my lack of experience with OOP, so I apologise if
that's the case!
I understand the need for the java.util.timer/Task classes to get the
correct things available to me.
I need to have a timer running in a background thread that calls a function
every say 1s. That function will do some things like updating a screen
message and some other application calculations. It also needs to stop the
timer when the necessary conditions are met. The timer will have been
started when the user requests.
My pseudo java code for this is something like:
void MainFunction()
{
if(UserMakesStartSelection == 1)
{
Timer timer = new CreateNewTimer( TimerFunctionToCall, StartNow,
Every1000ms)
}
}
void TimerFunctionToCall()
{
/* Gets called every 1000ms */
/* Do some things */
GetTimeElapsed();
UpdateScreen();
.
.
.
etc
/* Check conditions */
if(TimeToStop == 1)
{
StopTimer(timer);
}
}
All the examples I've seen seem to have these functions all wrapped together
in a class which makes it difficult to understand and follow. Like I say
it's probably my lack of OOP but if anyone can help to clear the muddy
waters I would appreciate it.