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