总结

thread

task

例子

刷新时间

  1. private void FreshTime()
  2. {
  3. #region 刷新事件
  4. Thread t = new Thread(delegate ()
  5. {
  6. isFresh = true;
  7. while (isFresh)
  8. {
  9. Invoke(new EventHandler(delegate
  10. {
  11. main_footer_lblTime.Text = GetDate() + DateTime.Now.ToString("HH:mm:ss");
  12. }));
  13. Thread.Sleep(500);
  14. }
  15. });
  16. t.IsBackground = false;
  17. t.Start();
  18. #endregion
  19. }

定时任务

  1. /// <summary>
  2. /// 自动灌溉程序线程
  3. /// </summary>
  4. Thread programIrrigateThread = null;
  5. public void ProgramThreadTask()
  6. {
  7. Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("==》自动化灌溉启动");
  8. programIrrigateThread = new Thread(ProgramThread);
  9. programIrrigateThread.Start();
  10. Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("==》自动化灌溉已运行");
  11. }
  12. private async void SensorThread()
  13. {
  14. #region 传感器线程
  15. if (GloalConfigs.EnableRuningSensor)
  16. {
  17. return;
  18. }
  19. GloalConfigs.EnableRuningSensor = true;
  20. var list = Panda.Common.Gatway.GloalConfigs.FinalDeviceList.ToList();
  21. var removeCacheTime = DateTime.Now;
  22. while (GloalConfigs.EnableRuningSensor)
  23. {
  24. #region 获取传感器数据
  25. try
  26. {
  27. #region 获取传感器数据
  28. if (Panda.Framework.Helper.TimeHelper.ExecDateDiffSecond(removeCacheTime, DateTime.Now) > 180) //600
  29. {
  30. list = Panda.Common.Gatway.GloalConfigs.FinalDeviceList.ToList();
  31. removeCacheTime = DateTime.Now;
  32. }
  33. if (list == null || list.Count == 0)
  34. {
  35. continue;
  36. }
  37. //Panda.Framework.Helper.ConsoleHelper.WriteInfoLine("执行传感器数据采集");
  38. var finalDeviceList = list.FindAll(m => GloalConfigs.Gatway.GatwayInfo.GatCode == m.GatwayCode);
  39. if (finalDeviceList == null || finalDeviceList.Count == 0)
  40. {
  41. continue;
  42. }
  43. foreach (var s in finalDeviceList)
  44. {
  45. await Task.Delay(500);
  46. switch (s.Category)
  47. {
  48. case "A00001002":
  49. #region 流量传感器
  50. if (s.Enable == 1)
  51. {
  52. await SensorOperatorByFlow(s);
  53. }
  54. #endregion
  55. break;
  56. }
  57. await Task.Delay(500);
  58. };
  59. #endregion
  60. }
  61. catch (Exception ex)
  62. {
  63. Panda.Framework.Helper.ConsoleHelper.WriteInfoLine($"SensorThread error,{ ex.Message }");
  64. }
  65. finally
  66. {
  67. Panda.Framework.Helper.ConsoleHelper.WriteInfoLine($"传感器数据下次采集时间,{DateTime.Now.AddMilliseconds(GloalConfigs.SleepRuningSensor).ToString("yyyy-MM-dd HH:mm:ss")}");
  68. await Task.Delay(GloalConfigs.SleepRuningSensor);
  69. }
  70. #endregion
  71. }
  72. #endregion
  73. }