Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » update the UI from a thread
update the UI from a thread [message #518214] Wed, 03 March 2010 14:00 Go to next message
Michael  is currently offline Michael Friend
Messages: 7
Registered: January 2010
Junior Member
Hi there,

i need to update a User Interface every 10 seconds with new data from a DB. The data must be stored in a session singleton, cause every user needs to get other informations.

I access the DB within a nonUIThreadWithFakeContext and I want to update the UI within display.asyncExec.

But the UI only updates after the user clicks on the UI (only then the display.asyncExec code is executed).

How to update the UI automatically (execute the display.asyncExec) without getting a No context available outside of the request service lifecycle or a Invalid thread access exception? Do I have to use the UICallBack#activate(String) method?

My code is:
Thread backgroundThread = new Thread(new Runnable()
{
    public void run()
    {
        while (true)
        {
              UICallBack.runNonUIThreadWithFakeContext(display, new Runnable()
              {
                    @Override
                    public void run()
                    {
                        // access the DB
                        objects = MySingleton.getInstance().getObjects();                    
                    }
                        
                    protected IStatus run(IProgressMonitor monitor)
                    {
                        return null;
                    }
              });
                    
              // schedule the UI update
              display.asyncExec(new Runnable()
              {
                    public void run()
                    {
                        viewer.setInput(objects);
                    }
              });

              try
              {
                    Thread.sleep(10000);
              }
              catch (Exception e) { }
        }
     }
});
backgroundThread.setDaemon(true);
backgroundThread.start();


Thanks in advance.

Michael
Re: update the UI from a thread [message #518262 is a reply to message #518214] Wed, 03 March 2010 15:26 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger HerrmannFriend
Messages: 581
Registered: July 2009
Senior Member
did you read the FAQ? In particualr these two entries?
*
http://wiki.eclipse.org/RAP/FAQ#How_to_update_the_UI_from_a_ background_thread_using_Jobs.3F
*
http://wiki.eclipse.org/RAP/FAQ#How_to_update_the_UI_from_a_ background_thread

HTH
Rüdiger

On 03.03.2010 15:00, Michael wrote:
> Hi there,
>
> i need to update a User Interface every 10 seconds with new data from a
> DB. The data must be stored in a session singleton, cause every user
> needs to get other informations.
> I access the DB within a nonUIThreadWithFakeContext and I want to update
> the UI within display.asyncExec.
> But the UI only updates after the user clicks on the UI (only then the
> display.asyncExec code is executed).
>
> How to update the UI automatically (execute the display.asyncExec)
> without getting a No context available outside of the request service
> lifecycle or a Invalid thread access exception? Do I have to use the
> UICallBack#activate(String) method?
> My code is:
> Thread backgroundThread = new Thread(new Runnable()
> {
> public void run()
> {
> while (true)
> {
> UICallBack.runNonUIThreadWithFakeContext(display, new Runnable()
> {
> @Override
> public void run()
> {
> // access the DB
> objects = MySingleton.getInstance().getObjects(); }
> protected IStatus run(IProgressMonitor monitor)
> {
> return null;
> }
> });
> // schedule the UI update
> display.asyncExec(new Runnable()
> {
> public void run()
> {
> viewer.setInput(objects);
> }
> });
>
> try
> {
> Thread.sleep(10000);
> }
> catch (Exception e) { }
> }
> }
> });
> backgroundThread.setDaemon(true);
> backgroundThread.start();
>
>
> Thanks in advance.
>
> Michael


--
Rüdiger Herrmann
http://eclipsesource.com
Re: update the UI from a thread [message #518539 is a reply to message #518214] Thu, 04 March 2010 12:19 Go to previous message
Michael  is currently offline Michael Friend
Messages: 7
Registered: January 2010
Junior Member
Hi,

yes, I read the FAQ before. I have found the solution now. The UICallBack has to be activated. The code is:

UICallBack.activate("ObjectsThread");
Thread backgroundThread = new Thread(new Runnable()
{
    public void run()
    {
        while (true)
        {
              UICallBack.runNonUIThreadWithFakeContext(display, new Runnable()
              {
                    @Override
                    public void run()
                    {
                        // access the DB and save the objects
                        MySingleton.getInstance().fetchCurrentObjects();                   
                    }
              });
                    
              // schedule the UI update
              display.asyncExec(new Runnable()
              {
                    public void run()
                    {
                        viewer.setInput(MySingleton.getInstance().getCurrentObjects());
                    }
              });

              try
              {
                    Thread.sleep(10000);
              }
              catch (Exception e) { }
        }
     }
});
backgroundThread.setDaemon(true);
backgroundThread.start();


Thanks anyway.

Michael
Previous Topic:[Browser] BrowserFunction does not work anymore
Next Topic:How to enable vertical scrolling for RAP application?
Goto Forum:
  


Current Time: Tue Apr 16 11:22:41 GMT 2024

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

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

Back to the top