Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Update diagram from background thread (Read 1722 times)
jcaron
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Jul 23rd, 2019
Update diagram from background thread
Jul 24th, 2019 at 1:45pm
Print Post  
Hi,

Is it possible to update a Diagram object in a separate thread? I have an operation where multiple nodes will be added/removed/updated on a user action and would like to push those actions to a separate thread to improve the user experience.

If this is not possible, maybe I'll create a duplicate diagram to apply the changes and then replace the diagram object in the DiagramView.

Thanks!
Jean
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Update diagram from background thread
Reply #1 - Jul 24th, 2019 at 2:08pm
Print Post  
Hi,

If it's graph layout classes taking most of the time for you, try their ArrangeAsync methods.

The Diagram class is definitely not thread-safe, but if you disable modifications on the DiagramView by setting Behavior = DoNothing, it might get safe enough to add items from a thread. Do let us know if it works Wink

Otherwise working with an offscreen copy and assigning it to DiagramView when the thread ends should be safe enough I guess.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3147
Joined: Oct 19th, 2005
Re: Update diagram from background thread
Reply #2 - Jul 24th, 2019 at 2:20pm
Print Post  
Setting Behavior will probably still be unsafe, because the worker thread could try modifying Diagram.Items while it's being iterated by drawing code in UI thread, and .NET throws exceptions for that. You can probably work around it by overriding DiagramView.OnPaint and synchronizing with the worker thread before calling base.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
jcaron
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 7
Joined: Jul 23rd, 2019
Re: Update diagram from background thread
Reply #3 - Jul 24th, 2019 at 2:43pm
Print Post  
Thanks Slavcho for yor quick answer.

The Diagram object does not allow changes from a separate thread (generate CrossThread exception), even with the Behaviour set to DoNothing and the DiagramView in a tab that is not displayed (so no paint event).

I used DoEvents to improve the responsiveness of the application and used the layout.ArrangeAsync. This is OK for small graph, but I will have to test how it behave when I reach a 1000 nodes.

Thanks for your support!
Jean
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint