StatesHandler.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using OpenSEMI.Core.Msg;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Windows;
  6. using System.Windows.Threading;
  7. namespace OpenSEMI.ClientBase.Handlers
  8. {
  9. public class StatesHandler : IHandler
  10. {
  11. private Func<List<string>, List<State>> GetStates;
  12. private static object lockObj = new object();
  13. public Dictionary<string, State> States
  14. {
  15. get;
  16. private set;
  17. }
  18. public MsgPool looper
  19. {
  20. get;
  21. private set;
  22. }
  23. private List<string> Keys
  24. {
  25. get;
  26. set;
  27. }
  28. private List<string> CommonKeys
  29. {
  30. get;
  31. set;
  32. }
  33. public StatesHandler(Func<List<string>, List<State>> funcGetStates, List<string> allkeys)
  34. {
  35. Keys = new List<string>();
  36. CommonKeys = new List<string>();
  37. States = new Dictionary<string, State>();
  38. looper = new MsgPool(500, Do, null, false);
  39. GetStates = funcGetStates;
  40. allkeys.ForEach(delegate(string key)
  41. {
  42. States.Add(key, new State
  43. {
  44. Key = key,
  45. DisplayValue = string.Empty
  46. });
  47. });
  48. }
  49. public void Handle()
  50. {
  51. looper.Run();
  52. }
  53. public void Do(MsgPool pool)
  54. {
  55. List<string> list = new List<string>();
  56. lock (lockObj)
  57. {
  58. if ((CommonKeys.Count != 0 || Keys.Count != 0) && Application.Current != null && Application.Current.Dispatcher != null)
  59. {
  60. if (CommonKeys.Count > 0)
  61. {
  62. list.AddRange(CommonKeys);
  63. }
  64. if (Keys.Count > 0)
  65. {
  66. list.AddRange(Keys);
  67. }
  68. goto end_IL_001c;
  69. }
  70. return;
  71. end_IL_001c:;
  72. }
  73. List<State> states = GetStates(list);
  74. Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
  75. {
  76. states.ForEach(delegate(State state)
  77. {
  78. if (States.ContainsKey(state.Key))
  79. {
  80. States[state.Key].DisplayValue = state.DisplayValue;
  81. }
  82. });
  83. });
  84. }
  85. public void Register(List<string> keys, bool isCommon = false)
  86. {
  87. lock (lockObj)
  88. {
  89. if (isCommon)
  90. {
  91. keys.ForEach(delegate(string e)
  92. {
  93. if (!CommonKeys.Contains(e))
  94. {
  95. CommonKeys.Add(e);
  96. }
  97. });
  98. }
  99. else
  100. {
  101. Keys.Clear();
  102. Keys.AddRange(keys);
  103. }
  104. }
  105. }
  106. public void UnRegister(List<string> keys)
  107. {
  108. lock (lockObj)
  109. {
  110. Keys.Clear();
  111. }
  112. }
  113. }
  114. }