java - 如何在 Eclipse RCP 应用程序中使用控制台 View

标签 java eclipse rcp

我刚刚开始使用 RCP 编写基于 Java 的应用程序。我正在尝试在我的应用程序中添加控制台 View ,并将 log4j 的信息输出到控制台。现在可以了。但是有一个问题,它不能像eclipse那样每行输出一次,而是在方法完成后输出所有信息。

Object[] elements = tableViewer.getCheckedElements();
    if(elements.length > 0){
        for(Object ele : elements){
            File file = (File) ele;
            logger.info("log4j处理目录" + file.getAbsolutePath());
            MessageConsoleStream stream = ConsoleFactory.getConsole().newMessageStream();
            stream.println("println处理目录" + file.getAbsolutePath());
            try {
                stream.flush();
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

我尝试使用stream.println()、stream.flush(),但它不起作用。 这是我第一次在 stackoverflow 上查询。对不起我的英语。

最佳答案

在用户界面线程中调用Thread.sleep(1000)将会阻塞整个UI,并且不会发生任何事情。切勿这样做。

如果您想每秒执行一次操作,请使用 DisplaytimerExec 方法来运行代码。

类似于:

Display.getDefault().timerExec(1000, new Runnable() {
   @Override
   public void run()
   {
     // TODO output one item to the log

     // TODO if need to run again call
     Display.getDefault().timerExec(1000, this);
   }
});

MessageConsoleStream 的 JavaDoc 说:

Clients should avoid writing large amounts of output to this stream in the UI thread. The console needs to process the output in the UI thread and if the client hogs the UI thread writing output to the console, the console will not be able to process the output.

因此,您必须不断循环输出到流,而不让 UI 线程中的其他代码运行。

关于java - 如何在 Eclipse RCP 应用程序中使用控制台 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36423024/

相关文章:

java - file.getName() 什么都不返回

java - 如何在同一个映射中使用 Converter 和 PropertyMap?

java - 类播异常 : Fragment_main cannot be cast to fragment activity android

c - Eclipse 构建的二进制文件太重

java - 如何使用注入(inject)器创建 GEF4 FXView/FXEditor

java - 如何让Minimax Alpha Beta算法发挥自己的作用?

java - sqs消息的changeMes​​sageVisibility函数如何延长可见时间?

java - 运行jdbc时出错

netbeans - 需要身份验证对话框

java - Eclipse RCP。添加 View 可恢复的附加条件