0

I have to display the progress bar while copying files. My code is displaying the progress bar. window is not responding while copying files.

Here my code:

Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                    try {
                        SetupDir = new File(SWT_Second.currentDir.getAbsolutePath().concat(File.separator).concat("setup"));
                        count=numberOfFiles(SetupDir);
                        logFileHandle.writeBytes("\nTotal Number of Files.... "+count);
                        System.out.println("NEW COUNT"+count);
                        copyFiles(SetupDir,dartBaseDir);
                        logFileHandle.writeBytes("copy over..: ");
                        copy_next_button.setEnabled(true);
                        actualCopyFlag=true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    };
                }
            });
         } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
});
t.start();

Method for copying files:

public static void copyFiles(final File srcDir,File destDir)  throws IOException{
String mkDir= null;
        File mkDir1=null;
        String cpDir= null;
        File cpDir1=null;
        pbar.setMinimum(0);
        pbar.setMaximum(count);

        for (File f : srcDir.listFiles()) {
            System.out.println("Copying for file : " + f.getAbsolutePath());
            if(f.isDirectory())
            {
                System.out.println("INSIDE DIRECTORY LOOP");
                System.out.println("It is a directory"+f.getName());
                cpDir=srcDir.getAbsolutePath().concat(File.separator).concat(f.getName());
                cpDir1=new File(cpDir);
                System.out.println("cpDir:  "+cpDir);
                mkDir = destDir.getAbsolutePath().concat(File.separator).concat(f.getName());

                mkDir1 = new File(mkDir);
                System.out.println("mkDir : "+mkDir);
                if( !mkDir1.exists() ) {
                    mkDir1.mkdir();
                }
                copyFiles(cpDir1, mkDir1);
}
            else 
            {   pb++;
            System.out.println("It is simple a file .....");
    File newFile = new File(destDir.getAbsolutePath().concat(File.separator).concat(f.getName()));
            System.out.println("newFile: "+newFile);

            Files.copy(Paths.get(f.getAbsolutePath()), Paths.get(newFile.getAbsolutePath()), StandardCopyOption.COPY_ATTRIBUTES);

                pbar.setSelection (pb+1);
                int x=pb+1;
                System.out.println("bar.setSelection (j+1): "+x );
                System.out.println("copy done for "+f.getAbsoluteFile() +" to "+newFile);
}
            System.out.println("Copy done for : " + f.getAbsoluteFile());
            }

        }
}

While copying files, the shell window is automatically not responding.

Help would appreciate..!!

Dude
  • 692
  • 1
  • 4
  • 25
  • 1
    Seems [that](http://stackoverflow.com/a/16714819/2894369) can help you. – alex2410 May 19 '15 at 06:54
  • possible duplicate of [SWT window becomes unresponsive while updating progressbar from a thread](http://stackoverflow.com/questions/30304309/swt-window-becomes-unresponsive-while-updating-progressbar-from-a-thread) – alex2410 May 19 '15 at 06:55
  • You are calling copyFiles method from Display thread only which is also busy in showing progress bar at the same time. Try to remove your copy code from Display thread and just put it in your newly started Thread main method. I didn't get why did you use Display thread asycnExec inside your new Thread? – Ishan Rastogi May 19 '15 at 07:00
  • One more thing- never use Display Thread or UI job to do non UI operations specially big one like copying files otherwise you will get hang issues in your application. – Ishan Rastogi May 19 '15 at 07:03
  • @IshanRastogi without Display thread, am getting invalid thread access error. That is why i have used. – mohamed asif May 19 '15 at 07:13
  • Please explain how my answer to [your previous](http://stackoverflow.com/questions/30075415/display-progress-bar-while-copying-files-in-only-swt-not-in-jface) (almost identical) question didn't solve the problem – Baz May 19 '15 at 08:04
  • 1
    You should use 2 separate threads. 1 for copying files by non UI thread and other for showing progress bar and updating it by Display thread so that you don't get the invalid thread access error. – Ishan Rastogi May 19 '15 at 08:39

1 Answers1

2

Your call to Display.getDefault().asyncExec is running your entire copy files operation in the user interface thread which causes it to become unresponsive.

Instead you should just call asyncExec each time you want to update the user interface. So

 ... copy file code in background thread

 Display.getDefault().asyncExec(new Runnable() {
       public void run() {
          pbar.setSelection (pb+1);
       }
  });
greg-449
  • 109,219
  • 232
  • 102
  • 145