简体   繁体   中英

How to use console view in Eclipse RCP Application

I just started using RCP to write Java-based applications. I am trying to add a console view in my app, and output info of log4j to the console. Now it works. But there is a problem, it cannot perform as eclipse witch output once per line, but output all info after the method finish.

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

I tried use stream.println() stream.flush(), but it does not work. It is my first time questing on stackoverflow. Sorry for my english.

Calling Thread.sleep(1000) in the User Interface Thread will block the entire UI and nothing will happen. Never do this.

If you want to do something once a second use the timerExec method of Display to run code.

Something like:

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

The JavaDoc for MessageConsoleStream says:

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.

So you must not loop constantly outputting to the stream without letting other code in the UI thread run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM