【问题标题】:Using ProgressMonitorDialog in Eclipse 4 properly在 Eclipse 4 中正确使用 ProgressMonitorDialog
【发布时间】:2012-10-10 19:34:19
【问题描述】:

我有一组执行文件操作的 API,例如saveToFile(CustomObject objectToSave);

由于文件操作可能很长,我决定应该向用户显示一些指示,例如进度条。

我读到了ProgressMonitorDialog,所以我尝试了它,但它并不能完全按照我的需要工作(或者更好的是我不知道如何正确使用它)。

目前我这样做:

ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(theShell);  
    try {  
        progressDialog.run(false, true, new IRunnableWithProgress() {  

        @Override  
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {  
            monitor.beginTask("Saving your data", 100);  
            try {  
                Utils.saveToFile(objectToSave);  
            } catch (Exception e) {  
            // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            monitor.done();   
        }  
     });   

这段代码非常快地显示了一个进度对话框并结束,但问题是在较慢的 PC 上,这会堆叠直到返回 Utils.saveToFile,而在保存完成之前我不知道如何指示中间进程。

我发现一个thread 提到了IProgressMonitor.UNKNOWN,但它没有说明monitorperformRead(_fileName, monitor); 期间发生的事情

我将如何解决这个问题?

【问题讨论】:

    标签: java eclipse swt eclipse-rcp jface


    【解决方案1】:

    ProgressMonitorDialog 是一段棘手的代码。我猜您缺少的部分是IProgressMonitor#worked(int),它将“增长”进度条。下面是一个代码示例,应该阐明如何使用它:

    public class Progress {
        public static void main(String[] args)
        {
            // Create your new ProgressMonitorDialog with a IRunnableWithProgress
            try {
                // 10 is the workload, so in your case the number of files to copy
                IRunnableWithProgress op = new YourThread(10);
                new ProgressMonitorDialog(new Shell()).run(true, true, op);
             } catch (InvocationTargetException ex) {
                 ex.printStackTrace();
             } catch (InterruptedException ex) {
                 ex.printStackTrace();
             }
        }
    
        private static class YourThread implements IRunnableWithProgress
        {
            private int workload;
    
            public YourThread(int workload)
            {
                this.workload = workload;
            }
    
            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
            {
                // Tell the user what you are doing
                monitor.beginTask("Copying files", workload);
    
                // Do your work
                for(int i = 0; i < workload; i++)
                {
                    // Optionally add subtasks
                    monitor.subTask("Copying file " + (i+1) + " of "+ workload + "...");
    
                    Thread.sleep(2000);
    
                    // Tell the monitor that you successfully finished one item of "workload"-many
                    monitor.worked(1);
    
                    // Check if the user pressed "cancel"
                    if(monitor.isCanceled())
                    {
                        monitor.done();
                        return;
                    }
                }
    
                // You are done
                monitor.done();
            }
    
        }
    }
    

    它看起来像这样:

    对于使用Utils.saveToFile 的特殊情况,您可以将IProgressMonitor 交给此方法,然后从那里调用worked() 方法。

    【讨论】:

    • +1。我使用FutureTask 并“轮询”isDone1 添加到monitor.worked(1); 以使任务栏增长。我想在更新 1 个文件时显示进度。但我想知道它是否不适合日食应用
    • @Cratylus 如果您能想到一个合适的方法来可视化文件更新,为什么不呢?我不认为这是不合适的,只要执行时间确实证明使用进度条是合理的。
    • as long as the execution time really justifies using a progress bar.有趣的评论。你的意思是在我的情况下可能太多了?还有什么其他选项更适合可能延迟的操作?
    • @Cratylus 我真的无法估计这个操作能持续多久。如果您自己事先不知道,请使用进度对话框。如果您确定这是一项可以在一两秒内完成的任务,那么我建议您只在另一个线程中完成它。顺便说一句:如果您不知道如何可视化进度,您可以随时使用问题中提到的IProgressMonitor.UNKNOWN
    • 我最好的猜测是它可能在 1-5 秒之间。
    猜你喜欢
    • 1970-01-01
    • 2012-09-08
    • 2012-09-04
    • 2018-09-10
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2013-02-18
    相关资源
    最近更新 更多