using OpenSEMI.Core.Msg; using System; using System.Collections.Generic; using System.Threading; using System.Windows; using System.Windows.Threading; namespace OpenSEMI.ClientBase.Handlers { public class StatesHandler : IHandler { private Func, List> GetStates; private static object lockObj = new object(); public Dictionary States { get; private set; } public MsgPool looper { get; private set; } private List Keys { get; set; } private List CommonKeys { get; set; } public StatesHandler(Func, List> funcGetStates, List allkeys) { Keys = new List(); CommonKeys = new List(); States = new Dictionary(); looper = new MsgPool(500, Do, null, false); GetStates = funcGetStates; allkeys.ForEach(delegate(string key) { States.Add(key, new State { Key = key, DisplayValue = string.Empty }); }); } public void Handle() { looper.Run(); } public void Do(MsgPool pool) { List list = new List(); lock (lockObj) { if ((CommonKeys.Count != 0 || Keys.Count != 0) && Application.Current != null && Application.Current.Dispatcher != null) { if (CommonKeys.Count > 0) { list.AddRange(CommonKeys); } if (Keys.Count > 0) { list.AddRange(Keys); } goto end_IL_001c; } return; end_IL_001c:; } List states = GetStates(list); Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { states.ForEach(delegate(State state) { if (States.ContainsKey(state.Key)) { States[state.Key].DisplayValue = state.DisplayValue; } }); }); } public void Register(List keys, bool isCommon = false) { lock (lockObj) { if (isCommon) { keys.ForEach(delegate(string e) { if (!CommonKeys.Contains(e)) { CommonKeys.Add(e); } }); } else { Keys.Clear(); Keys.AddRange(keys); } } } public void UnRegister(List keys) { lock (lockObj) { Keys.Clear(); } } } }