总结
thread
task
例子
刷新时间
private void FreshTime()
{
#region 刷新事件
Thread t = new Thread(delegate ()
{
isFresh = true;
while (isFresh)
{
Invoke(new EventHandler(delegate
{
main_footer_lblTime.Text = GetDate() + DateTime.Now.ToString("HH:mm:ss");
}));
Thread.Sleep(500);
}
});
t.IsBackground = false;
t.Start();
#endregion
}
定时任务
/// <summary>
/// 自动灌溉程序线程
/// </summary>
Thread programIrrigateThread = null;
public void ProgramThreadTask()
{
Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("==》自动化灌溉启动");
programIrrigateThread = new Thread(ProgramThread);
programIrrigateThread.Start();
Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("==》自动化灌溉已运行");
}
private async void SensorThread()
{
#region 传感器线程
if (GloalConfigs.EnableRuningSensor)
{
return;
}
GloalConfigs.EnableRuningSensor = true;
var list = Panda.Common.Gatway.GloalConfigs.FinalDeviceList.ToList();
var removeCacheTime = DateTime.Now;
while (GloalConfigs.EnableRuningSensor)
{
#region 获取传感器数据
try
{
#region 获取传感器数据
if (Panda.Framework.Helper.TimeHelper.ExecDateDiffSecond(removeCacheTime, DateTime.Now) > 180) //600
{
list = Panda.Common.Gatway.GloalConfigs.FinalDeviceList.ToList();
removeCacheTime = DateTime.Now;
}
if (list == null || list.Count == 0)
{
continue;
}
//Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("执行传感器数据采集");
var finalDeviceList = list.FindAll(m => GloalConfigs.Gatway.GatwayInfo.GatCode == m.GatwayCode);
if (finalDeviceList == null || finalDeviceList.Count == 0)
{
continue;
}
foreach (var s in finalDeviceList)
{
await Task.Delay(500);
switch (s.Category)
{
case "A00001002":
#region 流量传感器
if (s.Enable == 1)
{
await SensorOperatorByFlow(s);
}
#endregion
break;
}
await Task.Delay(500);
};
#endregion
}
catch (Exception ex)
{
Panda.Framework.Helper.ConsoleHelper.WriteInfoLine($"SensorThread error,{ ex.Message }");
}
finally
{
Panda.Framework.Helper.ConsoleHelper.WriteInfoLine($"传感器数据下次采集时间,{DateTime.Now.AddMilliseconds(GloalConfigs.SleepRuningSensor).ToString("yyyy-MM-dd HH:mm:ss")}");
await Task.Delay(GloalConfigs.SleepRuningSensor);
}
#endregion
}
#endregion
}