Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Trouble with threadsafe Databinding
Trouble with threadsafe Databinding [message #493895] Wed, 28 October 2009 08:28 Go to next message
René is currently offline René
Messages: 45
Registered: July 2009
Member
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 #493954 is a reply to message #493895] Wed, 28 October 2009 11:13 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew Hall
Messages: 368
Registered: July 2009
Senior Member
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
Re: Trouble with threadsafe Databinding [message #494131 is a reply to message #493954] Thu, 29 October 2009 06:27 Go to previous message
René is currently offline René
Messages: 45
Registered: July 2009
Member
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
Previous Topic:addFocusTracker for a Composite.
Next Topic:Manipulate strength and color of table/tree viewer grid?
Goto Forum:
  


Current Time: Thu Apr 24 15:03:26 EDT 2014

Powered by FUDForum. Page generated in 0.01709 seconds