Hello,
when I try to make my code more effective (faster) via parallel tasks I get Exceptions.
If I remove the parallel tasks everything works as it should.
What I am doing wrong and how can I make my code faster without exceptions.
when I try to make my code more effective (faster) via parallel tasks I get Exceptions.
If I remove the parallel tasks everything works as it should.
What I am doing wrong and how can I make my code faster without exceptions.
Here is the code sample:
public class LineManager : INotifyPropertyValueChanged
{
//.......
internal List<MyLine> HorizontalLine { get; } = new();
//.......
internal void RenderLines(IChart argichart, RenderContext argcontext)
{
List<MyLine>? _tempList = null;
lock (HorizontalLine) _tempList = new(HorizontalLine);
foreach (var line in _tempList)
{
DrawLines(argichart, argcontext, line, line.LineColor, line.LineType);
}
}
internal void AddLine(MyLine argline)
{
lock (HorizontalLine) HorizontalLine.Add(argline);
}
//.......
}
internal class MyClass()
{
public LineManager _lineManager { get; set; } = new();
private void AddLines()
{
_lineManager.AddLine(SupportLine);
}
public MyClass() {}
//Not sure what triggers this but it is used to update GUI
protected override void OnRender(RenderContext context, DrawingLayouts layout)
{
Parallel.Invoke(
() => _lineManager.RenderLines(ChartInfo!, context),
//and more ...
);
}
//Called when new data is available
protected override void OnCalculate(int bar, decimal value)
{
Parallel.Invoke(
() => AddLines(),
//and more ...
);
}
}
Last edited by a moderator: