Search code examples
javatimeoutswteclipse-rcprcp

how to implement a timeout in RCP Application/ SWT


I want to do 'logout' or "close" the application if the workbench is idle for an amount of time (30 mins, etc), but I don't know how to implement that yet. Does anyone has some suggestions?


Solution

  • I had a similar requirement, where I scrached my head to implement that.

    Requirement:

    Lock the Application Window, if idle for some pre-configured amount of time.

    But, mine was a pure SWT application where I had full control to Create and Dispose the Shell

    PFB the excerpt from my code, which is well documented, it may be helpful or provide an insight to you.

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

    Note

    I had a custom Implementation of java.util.Timer to provide the reschedule capability by cancelling the existing TimerTask, creating a new Timertask and then scheduling it again.