123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- 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<string>, List<State>> GetStates;
- private static object lockObj = new object();
- public Dictionary<string, State> States
- {
- get;
- private set;
- }
- public MsgPool looper
- {
- get;
- private set;
- }
- private List<string> Keys
- {
- get;
- set;
- }
- private List<string> CommonKeys
- {
- get;
- set;
- }
- public StatesHandler(Func<List<string>, List<State>> funcGetStates, List<string> allkeys)
- {
- Keys = new List<string>();
- CommonKeys = new List<string>();
- States = new Dictionary<string, State>();
- 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<string> list = new List<string>();
- 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<State> 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<string> 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<string> keys)
- {
- lock (lockObj)
- {
- Keys.Clear();
- }
- }
- }
- }
|