Invoking action after period of inactivity [message #660027] |
Wed, 16 March 2011 11:11  |
Eclipse User |
|
|
|
How do I make something happen only after a certain period of "inactivity"?
My problem is this. I have a view that displays information from a certain model. The model sends events when it updates, but sometimes can send a flood of events. Rather than causing my view to redraw on every single event, I want to make the event set a timer and only if that timer expires without another event coming in do I want to actually update my view. However I can't find a way of doing this. The best I can find is the Display.timerExec method, but I'd have to invoke that on each event and then check some timestamp to see whether another event has arrived in the mean time.
I've looked at the plain java Timer classes but they don't appear to provide me with a resettable timer either.
Anyone any ideas how I can achieve what I want to do?
|
|
|
|
|
Re: Invoking action after period of inactivity [message #661388 is a reply to message #660069] |
Thu, 24 March 2011 09:47  |
Eclipse User |
|
|
|
FWIW using Display.timerExec() may be a bit lighter-weight, demonstrated
below:
public class Main {
public static void main(String[] args) {
final int TIMEOUT = 5000;
final Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(10,10,200,200);
shell.setLayout(new FillLayout());
new Text(shell, SWT.MULTI);
final Runnable runnable = new Runnable() {
public void run() {
System.out.println("no activity for " + TIMEOUT + " ms");
display.timerExec(TIMEOUT, this);
}
};
Listener listener = new Listener() {
public void handleEvent(Event event) {
display.timerExec(-1, runnable); // cancel
display.timerExec(TIMEOUT, runnable);
}
};
display.addFilter(SWT.MouseDown, listener);
display.addFilter(SWT.MouseUp, listener);
display.addFilter(SWT.MouseMove, listener);
display.addFilter(SWT.MouseWheel, listener);
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.KeyUp, listener);
display.timerExec(TIMEOUT, runnable);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
Grant
"Tom Quarendon" <tomq@teamwpc.co.uk> wrote in message
news:ilqsrs$nrm$1@news.eclipse.org...
> Yes, that's the pattern I came up with in the end. The key is being able
> to cancel and reschedule the Job, so that it only goes off after there
> really has been the required delay.
>
|
|
|
Powered by
FUDForum. Page generated in 0.38121 seconds