Jump to content
Software FX Community

Change a Charts Series (or Data) from a different thread


Recommended Posts


I'm writing an application that changes the data in a ChartFX chart from another thread. It removes the series from the chart, then adds some new ones. These operations are called from a thread that did not create the chart. Currently i'm using this code to add a series to the chart:

  private delegate void ChartSeriesAddSafeHandler(Chart ch, SeriesAttributes s);   public static void ChartSeriesAddSafe(this Chart ch, SeriesAttributes s)   {   if (ch.Dispatcher.Thread == Thread.CurrentThread)   {   ch.Series.Add(s);   }   else   {   ch.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new ChartSeriesAddSafeHandler(ChartSeriesAddSafe), ch,s);   }   }


Using similar code to above, i'm able to set a chart's item source, clear the series, and set a series itemsource. However, adding a series to a chart fails with this exception:

Exception has been thrown by the target of an invocation.The calling thread cannot access this object because a different thread owns it.

I suspect theres some code inside of the chart object that isn't threadsafe. Any recommendations on how to get this working?


Link to comment
Share on other sites

It is my understanding that in WPF (not just ChartFX) only the thread that creates a UI object can access it.  I ran into a similar problem with some other code, and this is the response I received.  Unfortunately I can't find a link at the moment that explicitly states this, sorry. 

You can look at using Control.Dispatcher.Invoke or BeginInvoke to try to get your code working.  However, In my case, I ended up doing all of the non-UI calculations in a separate thread and then passing the data to the main thread to get my UI objects created. 

If I can find the right links, I'll post them.  Sorry I couldn't be of more help.  I just wanted to let you know that based on my experience with this issue I don't believe ChartFX is the root of the problem and that the WPF is responsible.  That isn't to say that ChartFX may not have a workaround for it. :D


Link to comment
Share on other sites

SeriesAttributes derives from DispatcherObject which gives it thread-affinity. The reason why SeriesAttributes derive from this class is that in order to allow you to create bindings between your UI and a Series it has to be either a FrameworkElement or a FrameworkContentElement.

I would recommend (as Dave did) you do your calculations/data retrieval on a different thread but then pass the data (and create additional series) on your UI thread.

If you can create a sample application that shows ChartFX not handling this correctly please send it to us.



Link to comment
Share on other sites

 Thanks Davidam and JuanC,

I realised my misdirection with my previous attempts: I was trying to change the properties of the chart from a different thread via Dispatcher.Invoke (one call to Invoke per property to change).

Instead, I needed to be calling Invoke on a method inside my UserControl from the different thread.

If anyone is having similar problems, reply back and ill post some code samples.



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...