I am using WinForms C# .NET 4.6
I have 3 Forms. Main, Form1 and Form2 Each form is running on a different thread. Form1 and Form2 have Arction LightningChart Control.. Each of those Forms has a method called "PlotUpdate" to update the charts with random values.
The Main Form has a timer that is triggered every 100ms. I want to call PlotUpdate from the Main Timer tick event to update the charts on Form1 and Form2. PlotUpdate method works in case the Arction LightningChart Control is placed in the Main Form, otherwise it doesn't.
How can I achieve this?
MainForm.cs
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace MultipleUIThreads
{
public partial class MainForm : Form
{
const int FormSeriesCount = 3;
static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
public MainForm()
{
InitializeComponent();
myTimer.Interval = 100;
myTimer.Tick += myTimer_Tick;
myTimer.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
// Start new UI thread for Form3
Thread thread1 = new Thread(() =>
{
Form1 f1 = new Form1();
Application.Run(f1);
});
thread1.Start();
// Start new UI thread for Form2
Thread thread2 = new Thread(() =>
{
Form2 f2 = new Form2();
Application.Run(f2);
});
thread2.Start();
}
private void myTimer_Tick(object sender, EventArgs e)
{
//What shall I write here
}
}
}
Form1.cs / Form2.cs are alike
using System;
using System.Windows.Forms;
using Arction.WinForms.Charting;
namespace MultipleUIThreads
{
public partial class Form1 : Form
{
int _pointsAdded = 0;
Random _rand = new Random();
public Form1()
{
InitializeComponent();
ChartCreator.InitChart(LChart);
}
public void PlotUpdate()
{
LChart.BeginUpdate();
double x = _pointsAdded * 0.1;
for (int i = 0; i < 1; i++)
{
SeriesPoint[] points = new SeriesPoint[1];
points[0].X = x;
points[0].Y = _rand.Next(-100, 100);
LChart.ViewXY.PointLineSeries[i].AddPoints(points, false);
}
LChart.ViewXY.XAxes[0].ScrollPosition = x;
LChart.EndUpdate();
_pointsAdded++;
}
}
}
ChartCreator.cs
using Arction.WinForms.Charting;
using Arction.WinForms.Charting.Axes;
using Arction.WinForms.Charting.SeriesXY;
using Arction.WinForms.Charting.Views.ViewXY;
namespace MultipleUIThreads
{
class ChartCreator
{
public static void InitChart(LightningChartUltimate ulChart)
{
ulChart.BeginUpdate();
ViewXY view = ulChart.ViewXY;
AxisX xAxis = view.XAxes[0];
xAxis.SetRange(0, 20);
xAxis.ScrollMode = XAxisScrollMode.Scrolling;
view.YAxes.Clear();
for (int seriesIndex = 0; seriesIndex < 1; seriesIndex++)
{
AxisY yAxis = new AxisY(view);
yAxis.SetRange(-100, 100);
view.YAxes.Add(yAxis);
PointLineSeries line = new PointLineSeries(view, xAxis, yAxis);
line.PointsVisible = false;
line.LineStyle.Color = DefaultColors.SeriesForBlackBackground[seriesIndex];
view.PointLineSeries.Add(line);
}
view.DropOldSeriesData = true;
view.AxisLayout.YAxesLayout = YAxesLayout.Stacked;
ulChart.EndUpdate();
}
}
}