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);
- Kevin Goos asked 8 years ago
- You must login to post comments
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
- Kevin Goos answered 8 years ago
- last edited 8 years ago
- You must login to post comments
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
- Andrew Burnett-Thompson answered 8 years ago
- You must login to post comments
Please login first to submit.