如何在长时间运行的Eclipse插件中管理控制台输出?

时间:2012-01-30 21:05:26

标签: eclipse multithreading eclipse-plugin console

我编写了一个可以运行的Eclipse插件。但是,在运行期间,不会显示任何控制台输出。仅当过程完成时,输出才会显示在控制台中。下面是我的处理程序,它显示为org.eclipse.ui.commands类型的扩展点:

public class MyHandler extends AbstractHandler {

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        ...
        MessageConsoleStream out = myConsole.newMessageStream();
        ...
        IConsoleView view = (IConsoleView) page.showView(id);
        view.display(myConsole);
        ...

        out.println("output that only shows up at the end");
        myConsole.activate();
        ...
        // Slow process
        ...
        out.println("everything is done");
        return null;
    }
}

因此,当进程运行时,控制台中没有任何内容。然后在最后,两个输出线都会进入视图。

我显然做错了控制台的事情,但我没有找到任何好的例子,也没有证明我的实验非常有成​​效。请指教。

3 个答案:

答案 0 :(得分:0)

您可以考虑使用ProgressMonitor(如果用户想要中止,可能会取消),这样用户就可以看到有事情发生了。

答案 1 :(得分:0)

这有效:

public class Merge extends AbstractHandler {
private static MessageConsole myConsole = null;
private static ExecutionEvent event = null;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Merge.event = event;

    //same idea as original post and other examples where it makes new or finds existing
    myConsole = makeConsole(Merge.event); 

    Job job = new Job("My Job Name"){
        @Override
        protected IStatus run(IProgressMonitor monitor){
            ...
            if (blah) {
                MessageConsoleStream out = myConsole.newMessageStream();
                out.println("output show up right away");
                ...
                // Slow process
                ...
                out.println("everything is done");
            } else {
                MessageDialog.openInformation(HandlerUtil.getActiveShell(Merge.event), "Information", "Please select valid file");
            }
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
    return null;
}
...
}

答案 2 :(得分:0)

也许你可以在每次out.print之后调用out.flush()......