0

i hope you are fine. i'm trying to make a progressBar increment while deleting multiple files in path, but the progressBar do not increase until it get the max directly, whats the problem, where i'm wrong ??

the Max of Progress Bar is listString of paths size that is 95 file, and the progress is the increment of counter, i hope you help me

this is my code im using :

FileUtil.listDir("/storage/emulated/0/Alarms/", listStr);
progressBar.setMax((int)listStr.size());
for(int i = 0; i < (int)(listStr.size()); i++) {
    if (FileUtil.isFile(listStr.get((int)(i)))) {
        FileUtil.deleteFile(listStr.get((int)(i)));
        
    }
    new Thread(new Runnable() {
        @Override 
      public void run() {

            progressBar.setProgress((int)i);
        }
    }).start();
}
Hasni
  • 187
  • 1
  • 10
  • Have you tried getting of the thread idea and just issue the `setProgress` on each iteration of loop? Starting up to 95 threads (even though they will quickly terminate) seems the wrong approach. Why the threads at all? –  May 16 '21 at 23:05
  • 2
    I agree with Andy. You should wrap this whole code block in a thread, and then update the progress bar using the event thread https://stackoverflow.com/questions/22772379/updating-ui-from-different-threads-in-javafx see here for how – Anthony Cathers May 17 '21 at 00:57
  • Your code seems logically wrong. If it is executed in the MainThread/UiThread then you're trying to delete 95 files from a BAD Thread because deletions are Synchronous task that could freeze the User Interface. Instead if that code is already executed in a separated thead, then managing a ProgressBar from a thread different than Main/UiThread is wrong because only MainThread can "touch" visible components.....So please tell us in WHICH Thread your cose is executed. – emandt May 17 '21 at 14:08

0 Answers0