【问题标题】:how to implement a timeout in RCP Application/ SWT如何在 RCP 应用程序/ SWT 中实现超时
【发布时间】:2013-06-29 00:03:39
【问题描述】:

如果工作台空闲一段时间,我想“注销”或“关闭”应用程序 时间(30分钟等),但我还不知道如何实现。有人有什么建议吗?

【问题讨论】:

    标签: java timeout swt eclipse-rcp rcp


    【解决方案1】:

    我也有类似的需求,所以我摸不着头脑来实现它。

    要求:

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

    但是,我是一个纯粹的 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 然后重新安排它来提供重新安排功能。

    【讨论】:

      【解决方案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
                  }
              });
      
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多