没有响应 - 在SWT中复制文件而不是在jFace中

时间:2015-05-19 06:48:00

标签: java multithreading file swt

我必须在复制文件时显示进度条。我的代码显示了进度条。复制文件时窗口没有响应。

这是我的代码:

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();

复制文件的方法:

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());
            }

        }
}

复制文件时,shell窗口自动无响应。

帮助会很感激.. !!

1 个答案:

答案 0 :(得分:2)

您对Display.getDefault().asyncExec的呼叫正在用户界面线程中运行整个复制文件操作,导致其无响应。

相反,每次要更新用户界面时都应该调用asyncExec。所以

 ... copy file code in background thread

 Display.getDefault().asyncExec(new Runnable() {
       public void run() {
          pbar.setSelection (pb+1);
       }
  });