Accessing SessionSingleton from thread [message #804553] |
Wed, 22 February 2012 19:10 |
|
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 #805338 is a reply to message #805028] |
Thu, 23 February 2012 16:42 |
|
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 |
|
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 |
|
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 #814375 is a reply to message #811813] |
Tue, 06 March 2012 11:25 |
|
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
|
|
|
Re: Accessing SessionSingleton from thread [message #816770 is a reply to message #814375] |
Fri, 09 March 2012 08:40 |
|
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 #816786 is a reply to message #816770] |
Fri, 09 March 2012 08:57 |
|
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 #816790 is a reply to message #816770] |
Fri, 09 March 2012 08:57 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.02856 seconds