Pre loader

Add verticallineannotations on a different thread

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

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.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies