SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy

We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

0
0

I am trying to add some annotations to my graph. The problem is that this function is called from a different thread so I get the following excepion:

I thried using an invoke on the main thread but this doesn’t help.

This is the code I use:

 var verticalLineAnnotation = new VerticalLineAnnotation
            {
                X1 = annotation.X,
                Tag = annotation,
                StrokeThickness = annotation.StrokeThickness,
                StrokeDashArray = annotation.StrokeDashArray,
                Stroke = annotation.Brush,
                ToolTip = annotation.LabelTooltip
            };

            var referenceDataAnnotation = annotation as ReferenceDataAnnotation;
            if (referenceDataAnnotation != null)
            {
                verticalLineAnnotation.ContextMenu = ChartContextDataTypesFactory.CreateContextMenus(referenceDataAnnotation.Type.Name, Application.Current.MainWindow);
            }

            if (IsChartFirst)
            {
                verticalLineAnnotation.AnnotationLabels = new ObservableCollection<AnnotationLabel>
                {
                    new AnnotationLabel
                    {
                        LabelPlacement = LabelPlacement.TopLeft,
                        FontSize = 12,
                        FontWeight = FontWeights.Normal,
                        Margin = new Thickness {Bottom = -5},
                        Text = annotation.LabelText
                    }
                };
            }
//This is the line where I get the exception
            ParentSurface.Annotations.Add(verticalLineAnnotation);
Version
4.2.2.9777
Images
  • You must to post comments
0
0

Hi Kevin

Annotations, and SciChartSurface, are WPF FrameworkElements. These can only be accessed on the thread they were created on.

Please see StackOverflow: WPF How to deal with Cross Thread Access Exceptions?

Best regards,
Andrew

  • You must to post comments
0
0

Hi Andrew

Thx for your quick response. I read the stackoverflow post you gave but this doesn’t give me a solution yet.
This is what I tried:

this.Dispatcher.Invoke(() =>
{
       ParentSurface.Annotations.Add(verticalLineAnnotation);
});
//Application.Current.Dispatcher.Invoke(() =>
//{
//    ParentSurface.Annotations.Add(verticalLineAnnotation);
//});
//DispatcherService.Invoke(() => {
//   ParentSurface.Annotations.Add(verticalLineAnnotation);
//});

For clarification for the ‘this.Dispatcher.Invoke()’, the class where I do this is of the following type:

public class AnnotationModifier : ChartModifierBase
{
}

Also as you can see on the image, my function does run on the main thread actually.

If it would help, I can include the full class or make a testproject.

Best regards,
Kevin

Images
  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.