-1

I am making a javafx application that simulates a robot vacuum.

I want it to be automated so it would vacuum the environment by itself.

I need to insert a delay so a human can see the steps the vacuum is taking as it traverses the environment.

So far all the delay methods I have tested crash my program if they are inside a while loop.

If I put it outside the while and just click a button for the next step, everything works fine.

It also works fine if I set the delay to really short time, like 1 ms.

Any ideas of why this is happening?

Jim Hayes
  • 2,126
  • 1
  • 15
  • 21
  • 3
    Please [edit] the question to include the code for at least the referenced while loop in the form of a [mre]. – Jason Aller Sep 22 '19 at 23:16
  • "Long running" operations (such as your delay) need to be done outside the UI thread. See https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm for more information of how to have such operations. – M. le Rutte Sep 24 '19 at 08:52

1 Answers1

0

Any application that executes a set of instructions for a while (is busy) and cannot respond to user input or system events is "seen" by Windows as "not responding" and when you try to interact with a "not responding" program, Windows will tell you it crashed.

The problem, you see, is that you try to delay interface updates with a while loop, and that makes your program execute something for a while and while is busy executing your loop it cannot respond to system or user events.

If you want to make delayed updates, use multithreading. Your while loop is blocking the main thread which is also responsible for rendering and taking any input, so you cannot block this thread. Create another thread and share state (eg. use observer pattern). And then you can execute TimeUnit's sleep() in this helper thread and it won't make your app "crash".

multicatch
  • 192
  • 4