using OpenSEMI.Core.Msg; using System; using System.Collections.Generic; using System.Threading; using System.Windows; using System.Windows.Threading; namespace OpenSEMI.ClientBase.UI { public class UIHandler : IHandler { private Dictionary acts; private static object lockObj = new object(); public MsgPool Looper { get; private set; } public UIHandler() { acts = new Dictionary(); Looper = new MsgPool(500, Do, null, false); } public void Handle() { Looper.Run(); } private void Do(MsgPool looper) { lock (lockObj) { if (Application.Current != null && Application.Current.Dispatcher != null) { Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { foreach (KeyValuePair act in acts) { act.Value(); } }); } } } public void Register(string key, Action act) { lock (lockObj) { if (!acts.ContainsKey(key)) { acts.Add(key, act); } } } public void UnRegister(string key) { lock (lockObj) { if (acts.ContainsKey(key)) { acts.Remove(key); } } } } }