20211010_CZPM_upperpc/垂直剖面动态观测系统/ViewModel/FirstPageViewModel.cs

129 lines
3.4 KiB
C#
Raw Normal View History

2023-07-27 03:01:29 +00:00
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<Task> tasklist = new List<Task>();
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);
}
/// <summary>
/// 用于结束当前的所有内部创建的线程
/// </summary>
public void Dispose()
{
try
{
//设置taskSwitch为false 停止线程内的方法
taskSwitch = false;
//等待线程内的当前方法执行完毕 就可以结束了
Task.WaitAll(this.tasklist.ToArray());
}
catch (Exception)
{
}
}
/// <summary>
/// 传感器数据获取
/// </summary>
public ObservableCollection<CourseSeriesModel> CourseSeriesList { get; set; } = new ObservableCollection<CourseSeriesModel>();
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);
}
}
}
}