Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Invoking action after period of inactivity
Invoking action after period of inactivity [message #660027] Wed, 16 March 2011 15:11 Go to next message
Tom Quarendon is currently offline Tom Quarendon
Messages: 23
Registered: July 2009
Junior Member
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 #660063 is a reply to message #660027] Wed, 16 March 2011 17:15 Go to previous messageGo to next message
Mauro Condarelli is currently offline Mauro Condarelli
Messages: 422
Registered: September 2009
Senior Member
Il 16/03/2011 16:11, Tom Quarendon ha scritto:
> 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?

I am using Jobs.
I don't know if this is the best strategy, but I do something like:

private Job saveJob;

public void notifyChanged(Notification msg) {
super.notifyChanged(msg);
if (updating)
return;
if (saveJob == null) {
saveJob = new Job("saveJob") {
@Override
protected IStatus run(IProgressMonitor monitor) {
...
return Status.OK_STATUS;
}
};
} else {
saveJob.cancel();
}
saveJob.schedule(saveTmpDelay);
}

HiH
Mauro
Re: Invoking action after period of inactivity [message #660069 is a reply to message #660063] Wed, 16 March 2011 17:50 Go to previous messageGo to next message
Tom Quarendon is currently offline Tom Quarendon
Messages: 23
Registered: July 2009
Junior Member
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.
Re: Invoking action after period of inactivity [message #661388 is a reply to message #660069] Thu, 24 March 2011 13:47 Go to previous message
Grant Gayed is currently offline Grant Gayed
Messages: 2151
Registered: July 2009
Senior Member
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.
>
Previous Topic:Problem With Images In The Help System
Next Topic:Unexpected context's restarts
Goto Forum:
  


Current Time: Thu Oct 02 00:40:20 GMT 2014

Powered by FUDForum. Page generated in 0.01718 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software