0

When the application is opened the user is faced with a log in screen, I need to know how to create a 5 minute timer when the scene is the log in screen and if no user has logged in after 5 mins, exit the application.

Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                System.exit(0);
            }
        };
        while (model.getUsername().equals("")) { //while user is not logged in
            timer.schedule(task, 1000 * 60 * 5);
        }
        timer.cancel();

I have created the code above but unsure why it would fit in the MVC approach to ensure everytime the user enters the log in scene, the timer starts.

  • 2
    Use `Timeline`. – SedJ601 Apr 05 '21 at 23:14
  • See if [this](https://stackoverflow.com/questions/27162374/javafx-2-user-idle-detection) helps. – SedJ601 Apr 05 '21 at 23:16
  • 2
    Your `while` loop is likely scheduling thousands of tasks. If you want to use a `Timer` then simply cancel it once the user logs in. However, I suggest following @Sedrick advice and use a `Timeline` or `PauseTransition` (i.e. the animation API) and just stop the animation if and when the user logs in. That will keep everything on one thread—the _JavaFX Application Thread_. The benefit of that is simpler implementation and less likely to encounter a timing issue. – Slaw Apr 06 '21 at 02:03

0 Answers0