sehcheese
sehcheese

Reputation: 175

IProgressMonitor .worked(int) not Updating Progress Bar

I'm using an IProgressMonitor for an Eclipse plugin (3.7). Various methods call .setTaskName(String) and .worked(int). When I run it, the task it's doing updates, but the green progress on the progress bar never advances. Below is some of the code:

IProgressMonitor.beginTask is called here, then doFinish is called and the monitor is passed along to it:

public boolean performFinish() {
    IRunnableWithProgress op = new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException {
            try {
                monitor.beginTask("Building project...", 10);
                doFinish(projectNamePage.getComposite(), new GridLayout(), monitor);
            } catch (CoreException e1) {
                e1.printStackTrace();
            } finally {
                monitor.done();
            }
        }
    };
    try {
        getContainer().run(false, false, op);
    } catch (InterruptedException e) {
        return false;
    } catch (InvocationTargetException e) {
        Throwable realException = e.getTargetException();
        MessageDialog.openError(getShell(), "Error", realException.getMessage());
        e.printStackTrace();
        return false;
    }
    return true;
}

The doFinish method delegates to other methods.

private void doFinish(Composite container, GridLayout layout, IProgressMonitor pmp) throws CoreException {
    //Get the root of the current Eclipse workspace
    IWorkspaceRoot iwr = ResourcesPlugin.getWorkspace().getRoot();

    //Create a new project with the name requested in SampleNewWizardPage.
    IProject project = iwr.getProject(projectNamePage.getFileName().split("\\.")[0]);
    IProjectDescription description = null;
    project.create(pmp);
    project.open(pmp);
    description = project.getDescription();
    pmp.worked(1);

    IJavaProject javaProject = setProjectDescription(description, project, pmp);
    setRawClasspathEntryList(pmp, javaProject);
    IFolder sourceFolder = buildSourceFolder(project, pmp);
    String jarFilePath = downloadAlgoraphJar(javaProject, iwr, pmp);

    buildAlgoSettingsFile(javaProject, iwr, pmp);

    populateClasspath(sourceFolder, javaProject, jarFilePath, pmp);
    buildPackageWithSkeleton(javaProject, sourceFolder, pmp);
}

Here's an example of one of the methods doFinish delegates to:

private void setRawClasspathEntryList(IProgressMonitor pmp, IJavaProject javaProject) {
    pmp.setTaskName("Building classpath...");
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.add(JavaRuntime.getDefaultJREContainerEntry());
    try {
        javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), pmp);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    pmp.worked(1);
}

Upvotes: 2

Views: 1570

Answers (1)

finejustice
finejustice

Reputation: 413

If you call run() in UI Thread, you must set fork to be true. (That is, runnable instance should not be run in UI Thread.)

UI thread handles UI event, like updating progress bar, redraw, etc. but if you run a time-consuming task in UI thread, the thread cannot handle the events because it is busy. (and event will be handled after the task completed)

Upvotes: 2

Related Questions