//LiveCharts https://lvcharts.net/ using LiveCharts; using LiveCharts.Wpf; using LiveCharts.WinForms; using LiveCharts.Configurations; using System.Windows.Forms; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using LiveCharts.Defaults; using LiveCharts.Geared; namespace CPFConsole { public class DataVisualizer { private readonly Form1 form; private double _trend; public DataVisualizer(Form1 form) { this.form = form; BellowsValues = new GearedValues().WithQuality(Quality.Highest); PressureValues = new GearedValues().WithQuality(Quality.Highest); ModelPressureValues = new GearedValues().WithQuality(Quality.Highest); PlatformVelocity = new GearedValues().WithQuality(Quality.Highest); ModelVelocity = new GearedValues().WithQuality(Quality.Highest); //var time = new List(); BellowsNewDataReady = false; var now = DateTime.Now; } public bool IsReading { get; set; } public GearedValues BellowsValues { get; set; } public GearedValues PressureValues { get; set; } public GearedValues ModelPressureValues { get; set; } public GearedValues PlatformVelocity { get; set; } public GearedValues ModelVelocity { get; set; } public double Count { get; set; } public double CurrentLecture { get; set; } public bool IsHot { get; set; } public bool BellowsNewDataReady { get; set; } public bool PressureNewDataReady { get; set; } public bool PVNewDataReady { get; set; } public bool ModelVelocityNewDataReady { get; set; } public void Stop() { IsReading = false; } public void Clear() { BellowsValues.Clear(); PressureValues.Clear(); ModelPressureValues.Clear(); PlatformVelocity.Clear(); } public void Read() { if (IsReading) return; //lets keep in memory only the last 20000 records, //to keep everything running faster const int keepRecords = 20000; IsReading = true; Action readBellows = () => { while (IsReading) { if (BellowsNewDataReady) { //when multi threading avoid indexed calls like -> Values[0] //instead enumerate the collection //ChartValues/GearedValues returns a thread safe copy once you enumerate it. //TIPS: use foreach instead of for //LINQ methods also enumerate the collections // var first = BellowsValues.DefaultIfEmpty(0).FirstOrDefault(); //if (BellowsValues.Count > keepRecords - 1) BellowsValues.Remove(first); BellowsValues.Add(new DateTimePoint(System.DateTime.Now ,Convert.ToDouble(form.Globals.GlobalBellowsPos))); IsHot = _trend > 0; Count = BellowsValues.Count; CurrentLecture = _trend; BellowsNewDataReady = false; //Thread.Sleep(1500); } } }; Action readPressure = () => { while (IsReading) { if (PressureNewDataReady) { //when multi threading avoid indexed calls like -> Values[0] //instead enumerate the collection //ChartValues/GearedValues returns a thread safe copy once you enumerate it. //TIPS: use foreach instead of for //LINQ methods also enumerate the collections //var first = PressureValues.DefaultIfEmpty(0).FirstOrDefault(); //if (PressureValues.Count > keepRecords - 1) PressureValues.Remove(first); //if (PressureValues.Count < keepRecords) PressureValues.Add(Convert.ToDouble(form.Globals.GlobalPressure)); PressureValues.Add(new DateTimePoint(System.DateTime.Now, Convert.ToDouble(form.Globals.GlobalPressure))); ModelPressureValues.Add(new DateTimePoint(System.DateTime.Now, Convert.ToDouble(form.Globals.GlobalModelPressure))); IsHot = _trend > 0; Count = PressureValues.Count; CurrentLecture = _trend; PressureNewDataReady = false; //Thread.Sleep(1500); } } }; Action readPlatformVelocity = () => { while (IsReading) { if (PVNewDataReady) { //when multi threading avoid indexed calls like -> Values[0] //instead enumerate the collection //ChartValues/GearedValues returns a thread safe copy once you enumerate it. //TIPS: use foreach instead of for //LINQ methods also enumerate the collections PlatformVelocity.Add(new DateTimePoint(System.DateTime.Now,Convert.ToDouble(form.Globals.GlobalPlatformVelocity))); ModelVelocity.Add(new DateTimePoint(System.DateTime.Now, Convert.ToDouble(form.Globals.GlobalModelVelocity))); IsHot = _trend > 0; Count = PlatformVelocity.Count; CurrentLecture = _trend; PVNewDataReady = false; //Thread.Sleep(500); } } }; Action readModelVelocity = () => { while (IsReading) { if (ModelVelocityNewDataReady) { //when multi threading avoid indexed calls like -> Values[0] //instead enumerate the collection //ChartValues/GearedValues returns a thread safe copy once you enumerate it. //TIPS: use foreach instead of for //LINQ methods also enumerate the collections ModelVelocity.Add(new DateTimePoint(System.DateTime.Now, Convert.ToDouble(form.Globals.GlobalModelVelocity))); IsHot = _trend > 0; Count = ModelVelocity.Count; CurrentLecture = _trend; ModelVelocityNewDataReady = false; Thread.Sleep(250); } } }; //2 different tasks adding a value every ms //add as many tasks as you want to test this feature Task.Factory.StartNew(readBellows); Task.Factory.StartNew(readPressure); //Task.Factory.StartNew(readModelPressure); Task.Factory.StartNew(readPlatformVelocity); //Task.Factory.StartNew(readModelVelocity); //Task.Factory.StartNew(readFromTread); //Task.Factory.StartNew(readFromTread); //Task.Factory.StartNew(readFromTread); } } }