如何在RCP应用程序/ SWT中实现超时

时间:2013-06-29 00:03:39

标签: java timeout swt eclipse-rcp rcp

如果工作台空闲,我想要“注销”或“关闭”应用程序 时间(30分钟等),但我不知道如何实施,有没有人有一些建议?谢谢你提前!

2 个答案:

答案 0 :(得分:3)

我有一个类似的要求,我在那里实施了这个要求。

要求:

  

如果空闲一段预先配置的时间,请锁定应用程序窗口。

但是,我是一个纯粹的SWT application,我完全控制CreateDispose Shell

PFB摘自我的代码,该代码有详细记录,可能对您有帮助或提供见解。

    //Boilerplate code for SWT Shell
   //Event loop modified in our case
   boolean readAndDispatch;
        while (!shell.isDisposed()) { //UI Thread continuously loop in Processing and waiting for Events until shell is disposed.
            try {
                readAndDispatch = shell.getDisplay().readAndDispatch(); // reads an Event and dispatches, also returns true if there is some work else false.
                if (!readAndDispatch) { //There is no work to do, means, System is idle
                    timer.schedule(worker, delay); // So, schedule a Timer which triggers some work(lock or logout code) after specified time(delay)
                    shell.getDisplay().sleep(); 
                } else{
                    timer.reschedule(delay); //This means System is not idle. So, reset your Timer
                }
            } catch (Throwable e) {
                log.error(ExceptionUtils.getStackTrace(e));
                MessageDialog.openError(shell, "Error !!", ExceptionUtils.getRootCauseMessage(e));

            }
        }

注意

我有java.util.Timer的自定义实施,通过取消现有TimerTask,创建新Timertask然后再次安排来提供重新安排功能。

答案 1 :(得分:2)

我在RCP应用程序中做了同样的事情。在Application.java类中添加以下行。

Display display = PlatformUI.createDisplay();
display.addFilter(SWT.MouseDown, new TimerListener());
display.addFilter(SWT.MouseMove, new TimerListener());
display.addFilter(SWT.KeyDown, new TimerListener());

class TimerListener implements Listener 
{
public void handleEvent(Event e) 
  {      
    if (Resources.timer != null ) 
    {
     // Restart the timer on any UI interactions
        Resources.timer.restart();
    }
  }   
} 

这是计时器实现。我正在使用Swing计时器但你 可以使用SWT计时器。

Resources.timer = new javax.swing.Timer(1000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        Display.getDefault().asyncExec(new Runnable() {
            @Override
            public void run() {                 
                // Log off you application here
            }
        });

    }
});