Trouble with threadsafe Databinding [message #493895] |
Wed, 28 October 2009 08:28  |
Eclipse User |
|
|
|
Hi all,
i had a little bit trouble with to make my Observable threadsafe. Now i
found a solution and i just want to ask if it is appropriate.
I extended the AbstractObservable-Class and declared a
fireGeneralChange()-method that is threadsafe.
Code:
public class MyObservable extends AbstractObservable {
public AutegObservable() {
super(Realm.getDefault());
}
public void fireGeneralChange(final GeneralEvent event){
if(!getRealm().isCurrent()){
getRealm().asyncExec(new Runnable() {
@Override
public void run() {
fireEvent(event);
}
});
} else {
fireEvent(event);
}
}
}
My concrete problem was that, that i delegate a long term operation to a
separate thread. But this operation fires events which refresh the GUI.
Without checking the current realm i always get a
TargetInvocationException from my GUI controls.
I just want to ask if my solution is appropriate and don't come with a
bunch of disadvantages.
Thanks in advance.
Regards,
René
|
|
|
|
Re: Trouble with threadsafe Databinding [message #494131 is a reply to message #493954] |
Thu, 29 October 2009 06:27  |
Eclipse User |
|
|
|
Hi Matthew,
thank you for your quick and interesting answer. I'll take it to heart.
Cheers,
René
Matthew Hall wrote:
> René wrote:
>> My concrete problem was that, that i delegate a long term operation to
>> a separate thread. But this operation fires events which refresh the GUI.
>> Without checking the current realm i always get a
>> TargetInvocationException from my GUI controls.
>
> You can call getRealm().exec(Runnable) which already implements the "if
> realm is current then execute runnable now else queue runnable for
> asynchronous execution" logic for you, that might save you a few lines
> of code. Other than that there doesn't appear to be anything wrong with
> your approach.
>
> If your worker thread is event-driven (as opposed to just executing a
> long-running operation) you may benefit from implementing a realm class
> for your worker thread, which would allow you to use DataBindingContext
> to take care of the synchronization between the worker thread and the UI
> thread. See the ThreadRealm class in the databinding tests project for
> a good starting point.
>
> Matthew
|
|
|
Powered by
FUDForum. Page generated in 0.18400 seconds