Better way to update UI with a job [message #629920] |
Wed, 29 September 2010 19:54  |
Eclipse User |
|
|
|
hi all
i have a job (and a uijob) that update my UI. but i was thinking if there's a better wat to do this.
actually, i'm doing like this:
public class CarregarMapaJob extends Job
{
private Location loc;
private Image img;
private AhmCastView view;
public CarregarMapaJob(String name, Location loc, AhmCastView view) {
super(name);
this.loc = loc;
this.view = view;
}
@Override
protected IStatus run(IProgressMonitor monitor)
{
try
{
monitor.beginTask("Criando URL e baixando mapa", -1);
String center = loc.getLatitude()+","+loc.getLongitude();
String def = "http://maps.google.com/staticmap?center="+center+"&zoom=12&" +
"size=800x800&key="+DataUtils.GMAP_KEY+"&sensor=true";
URL url = new URL(def);
img = ImageDescriptor.createFromURL(url).createImage();
monitor.done();
} catch (Exception e)
{
e.printStackTrace();
}
finally {
UIJob uiCarregarMapa = new UIJob("Carregando mapa") {
public IStatus runInUIThread(IProgressMonitor monitor)
{
try
{
monitor.beginTask("Adicionando mapa", -1);
view.addMapaLabel(img);
monitor.done();
} catch (Exception e)
{
e.printStackTrace();
}
return Status.OK_STATUS;
}
};
uiCarregarMapa.setUser(true);
uiCarregarMapa.schedule();
}
return Status.OK_STATUS;
}
as you can see, i'm passing the view as a parameter to the constructor, and inside the UIJob, I update her (the ui, of course)
is this a good approach?
sorry for the english.
thanks
|
|
|
|
Re: Better way to update UI with a job [message #634162 is a reply to message #629943] |
Wed, 20 October 2010 12:12  |
Eclipse User |
|
|
|
Prakash G.R.is right. But i will add that its recommended to use Display.asynchExec (cause using synch will force Display to run all requests it has queued to run yours).
in your code would look like
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
view.addMapaLabel(img);
}
})
That will run the call to addMapaLabel in the UI thread.
Regards
|
|
|
Powered by
FUDForum. Page generated in 0.06091 seconds