TimedData.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace monitor_main_windows
  8. {
  9. public class TimedData<Data_type>
  10. {
  11. private Data_type m_data;
  12. private DateTime m_time_point;
  13. private double m_time_out_ms = 3000;
  14. private object m_lock = new object();
  15. public TimedData(Data_type data)
  16. {
  17. m_data = data;
  18. m_time_point = DateTime.Now;
  19. }
  20. public static implicit operator TimedData<Data_type>(Data_type data)
  21. {
  22. return new TimedData<Data_type>(data);
  23. }
  24. public bool IsTimeout()
  25. {
  26. DateTime time = DateTime.Now;
  27. if((time-m_time_point).TotalMilliseconds> m_time_out_ms)
  28. {
  29. return true;
  30. }
  31. return false;
  32. }
  33. public Data_type Value
  34. {
  35. get
  36. {
  37. return m_data;
  38. }
  39. }
  40. public void Set_timeout_ms(int ms=1000)
  41. {
  42. lock (m_lock)
  43. {
  44. m_time_out_ms = ms;
  45. }
  46. }
  47. }
  48. }