using LiveCharts; using LiveCharts.Defaults; using LiveCharts.Wpf; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using 垂直剖面动态观测系统.Common; using 垂直剖面动态观测系统.DataAccess; using 垂直剖面动态观测系统.Model; namespace 垂直剖面动态观测系统.ViewModel { public class FirstPageViewModel : NotifyBase { //propfull private int _instrumentValue = 0; public int InstrumentValue { get { return _instrumentValue; } set { _instrumentValue = value; this.DoNotify(); } } private int _itemCount; public int ItemCount { get { return _itemCount; } set { _itemCount = value; this.DoNotify(); } } private string _portName; public string PortName { get { return _portName; } set { _portName = value; this.DoNotify(); } } private int _baudRate; public int BaudRate { get { return _baudRate; } set { _baudRate = value; this.DoNotify(); } } private int myVar; public int MyProperty { get { return myVar; } set { myVar = value; } } GlobalValues GlobalValues = new GlobalValues(); //赋值 public FirstPageViewModel() { this.RefreshInitInstrumentValue(); //this.InitCouseSeries(); Console.WriteLine(); } Random random = new Random(); //当taskSwitch为true的时候线程内的方法会执行 为false的时候方法不会执行 bool taskSwitch = true; //创建一个task的list List tasklist = new List(); private void RefreshInitInstrumentValue() { var task = Task.Factory.StartNew(new Action(async () => { while (taskSwitch) { InstrumentValue = random.Next(Math.Max(this.InstrumentValue - 5, -10), Math.Min(this.InstrumentValue + 5, 90)); //停顿1秒 await Task.Delay(1000); } })); //将当前task放入list中 tasklist.Add(task); } /// /// 用于结束当前的所有内部创建的线程 /// public void Dispose() { try { //设置taskSwitch为false 停止线程内的方法 taskSwitch = false; //等待线程内的当前方法执行完毕 就可以结束了 Task.WaitAll(this.tasklist.ToArray()); } catch (Exception) { } } /// /// 传感器数据获取 /// public ObservableCollection CourseSeriesList { get; set; } = new ObservableCollection(); public void InitCouseSeries() { var cList = LocalDataAccess.GetInstance().GetCoursePlayRecord(); //查询列表中的最大数量值作为itemcount //this.ItemCount = cList.Max(c => c.SeriesList.Count()); foreach (var item in cList) { this.CourseSeriesList.Add(item); } } } }