Skip to main content



      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 09:00 Go to next message
Eclipse UserFriend
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 10:26 Go to previous messageGo to next message
Eclipse UserFriend
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 07:19 Go to previous message
Eclipse UserFriend
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 Mar 11 07:22:24 EDT 2025

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

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

Back to the top