简体   繁体   中英

How to run multiple thread properly in eclipse plugin?

My plugin checks if the user is idle for a certain amount of time. With the solution from here , I tried to work my way around. But the eclipse application becomes unresponsive until the loop ends.

Also, the message box is just a plain box with no title and buttons. Can somebody tell me what is wrong with this code?

@Override
    public void earlyStartup() {
        Display.getDefault().asyncExec(new Runnable() {
            public void run() {                                                              
                //while(true) {
                for (stop=System.nanoTime()+TimeUnit.MINUTES.toNanos(1);stop>System.nanoTime();) {
                Display.getDefault().addFilter(SWT.KeyUp, new Listener() {

                    @Override
                    public void handleEvent(Event event) {
stop=System.nanoTime()+TimeUnit.MINUTES.toNanos(1);
                                System.out.println("checkpoint 1");
}

                });
                }
                Shell shell = new Shell(Display.getDefault());
                 MessageBox dialog =
                            new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
                        dialog.setText("Alert!");
                        dialog.setMessage("You have been idle for the last 3 minutes.");
                        shell.open();

stop=System.nanoTime()+TimeUnit.MINUTES.toNanos(1);
                        System.out.println("checkpoint 2");

            }
        });
}

Display.asyncExec does not run code in a separate thread. It runs the code in the main UI thread as soon as it is available. The UI thread will be blocked until the code ends.

Instead you can run code in a normal Java thread. But you must call asyncExec to execute any UI code you want to run from the thread.

In your actual code you should only be calling Display.addFilter once . This adds a listener which will be called every time the key up event occurs from then onwards. Since this is UI code you can't actually run this in a background thread at all.

So you can't use a loop like you have shown. You have to keep track of things in the key listener, updating each time the listener is called.

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