Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Accessing SessionSingleton from thread
Accessing SessionSingleton from thread [message #804553] Wed, 22 February 2012 19:10 Go to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Hi guys,

I am trying to access my sessionsingleton from a background thread, the thing is I get a null value when I call Display.getDefault() method. Is it correct to do this?


final Display display = Display.getCurrent();
final Runnable runnable = new Runnable() {
public void run() {
UICallBack.runNonUIThreadWithFakeContext(display,new Runnable() {
public void run() {
NMSessionSingleton sessionSingleton = NMSessionSingleton.getInstance();
... Do something ...
}
});
}
}
};
new Thread(runnable).start();

What am I doing wrong??
Re: Accessing SessionSingleton from thread [message #804981 is a reply to message #804553] Thu, 23 February 2012 08:03 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 1859
Registered: July 2009
Senior Member
Hi David,
probably, you try to get the display from a non UI thread too.
Best,
Ivan

On 2/22/2012 9:10 PM, David Casas wrote:
> Hi guys,
>
> I am trying to access my sessionsingleton from a background thread,
> the thing is I get a null value when I call Display.getDefault()
> method. Is it correct to do this?
>
>
> final Display display = Display.getCurrent();
> final Runnable runnable = new Runnable() {
> public void run() {
> UICallBack.runNonUIThreadWithFakeContext(display,new Runnable() {
> public void run() {
> NMSessionSingleton sessionSingleton =
> NMSessionSingleton.getInstance();
> ... Do something ...
> }
> });
> }
> }
> };
> new Thread(runnable).start();
>
> What am I doing wrong??

--
Ivan Furnadjiev

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: Accessing SessionSingleton from thread [message #805025 is a reply to message #804981] Thu, 23 February 2012 09:02 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Well, the thing is I am a bit newbie in the RAP Context. It is a non-UI Thread and I want to send some information to the users...so how can I achieve that? Is there any other way to update users UI from a non-UI Thread?

Cheers
Re: Accessing SessionSingleton from thread [message #805028 is a reply to message #804981] Thu, 23 February 2012 09:02 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Well, the thing is I am a bit newbie in the RAP Context. It is a non-UI Thread and I want to send some information to the users...so how can I achieve that? Is there any other way to update users UI from a non-UI Thread?

Cheers
Re: Accessing SessionSingleton from thread [message #805114 is a reply to message #805028] Thu, 23 February 2012 10:48 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 1859
Registered: July 2009
Senior Member
Hi David,
please look at these FAQ 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
In both cases you have to provide a display instance from the UI thread
- use someControl.getDisplay() for example. Display.getCurrent() and
Display.getDefault() should work too, but only if they are called from
the UI thread.
HTH,
Ivan

On 2/23/2012 11:02 AM, David Casas wrote:
> Well, the thing is I am a bit newbie in the RAP Context. It is a
> non-UI Thread and I want to send some information to the users...so
> how can I achieve that? Is there any other way to update users UI from
> a non-UI Thread?
>
> Cheers

--
Ivan Furnadjiev

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: Accessing SessionSingleton from thread [message #805338 is a reply to message #805028] Thu, 23 February 2012 16:42 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1295
Registered: July 2009
Senior Member

Hi,

The Display "sticks" to the UI thread and Display.getCurrent() will only
return the Display when called on the UI thread. Keep in mind that
there are many different displays on the system - one for every user
session - and the method can only decide which is "yours" by looking at
the thread. When on a different thread, you have to keep a reference to
the display you want to access.

To access the UI from a different thread, you have to put a runnable in
Display#asyncExec(). When you don't need to access the UI, but only
need a session singleton's instance, then you can run the runnable in a
fake context, using the UICallBack method with the funny name.

HTH, Ralf

--
Ralf Sternberg

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: Accessing SessionSingleton from thread [message #811604 is a reply to message #805338] Fri, 02 March 2012 15:37 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Hi ralf,

so if I want to do a different action for each user using the data stored in the SessionSingleton I have to have an array of Displays of the connected users?? I mean, there is no other way to do an action based on the Session on a background thread?

Thanks a lot
Re: Accessing SessionSingleton from thread [message #811607 is a reply to message #805338] Fri, 02 March 2012 15:37 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Hi ralf,

so if I want to do a different action for each user using the data stored in the SessionSingleton I have to have an array of Displays of the connected users?? I mean, there is no other way to do an action based on the Session on a background thread?

Thanks a lot
Re: Accessing SessionSingleton from thread [message #811813 is a reply to message #811604] Fri, 02 March 2012 21:29 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf SternbergFriend
Messages: 1295
Registered: July 2009
Senior Member

Hi David,

keeping references of Displays would prevent these Displays from being
garbage collected after session termination. You should rather add some
kind of listener to the background job from every active session. Each
listeners would keep a reference to its Display to be able to access the
UI. On the end of a session, the listener should be removed.

Hope this helps,
Ralf

--
Ralf Sternberg

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: Accessing SessionSingleton from thread [message #814375 is a reply to message #811813] Tue, 06 March 2012 11:25 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Well Ralph,

I think I would have the same problem here, If I do not unregister the listeners I will have reference to the Displays in those listeners and the Displays won't be garbage collected... however maybe it is the right way to do it.

I will see what I can do Wink
Re: Accessing SessionSingleton from thread [message #816770 is a reply to message #814375] Fri, 09 March 2012 08:40 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Hi,

finally I was able to update UI from a background thread thanks to Ralph instructions. The problem is it is not updated on real time, I mean I do an display.asyncExec which is not executed til I perform some movement on the Browser. I don't know how to make it dynamically, I've tried to use UICallback.activate inside the bgRunnable of UICallBack.runNonUIThreadWithFakeContext(display, bgRunnable)and it said I am in the wrong thread (if I do it inside an asyncExec it wont be call til I move something on the screen).

Could you explain me if I can update a View on real time??

Thanks in advance!
Re: Accessing SessionSingleton from thread [message #816785 is a reply to message #816770] Fri, 09 March 2012 08:56 Go to previous messageGo to next message
Amandeep Chahal is currently offline Amandeep ChahalFriend
Messages: 85
Registered: September 2011
Location: India
Member
The UICallBack.activate needs to be done from the UI thread. Thats the reason your getting the wrong thread error. Once you have the UICallback activated from there, your view will then be updated realtime.
Re: Accessing SessionSingleton from thread [message #816786 is a reply to message #816770] Fri, 09 March 2012 08:57 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Well I've found that calling UICallBack.activate(id) in the creation on the Display is solving this issue, is it right to do it there? (the background thread is always in the background of the application). Wich id should I use? is it independent of the session?

Thanks
Re: Accessing SessionSingleton from thread [message #816789 is a reply to message #816770] Fri, 09 March 2012 08:56 Go to previous messageGo to next message
Amandeep Chahal is currently offline Amandeep ChahalFriend
Messages: 85
Registered: September 2011
Location: India
Member
The UICallBack.activate needs to be done from the UI thread. Thats the reason your getting the wrong thread error. Once you have the UICallback activated from there, your view will then be updated realtime.
Re: Accessing SessionSingleton from thread [message #816790 is a reply to message #816770] Fri, 09 March 2012 08:57 Go to previous messageGo to next message
David Casas is currently offline David CasasFriend
Messages: 58
Registered: November 2011
Location: Barcelona
Member

Well I've found that calling UICallBack.activate(id) in the creation on the Display is solving this issue, is it right to do it there? (the background thread is always in the background of the application). Wich id should I use? is it independent of the session?

Thanks
Re: Accessing SessionSingleton from thread [message #816845 is a reply to message #816790] Fri, 09 March 2012 10:29 Go to previous message
Amandeep Chahal is currently offline Amandeep ChahalFriend
Messages: 85
Registered: September 2011
Location: India
Member
1. You can call UICallback.activate before starting your background thread.
2. You should make it session specific (have it activated for every session)
3. Use some session unique attribute as key

Previous Topic:New RichText support in Tables
Next Topic:Display.setFocusControl in RAP
Goto Forum:
  


Current Time: Fri Nov 28 00:42:13 GMT 2014

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

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