UIHandler.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Threading;
  8. using System.Threading;
  9. using OpenSEMI.Core.Msg;
  10. namespace OpenSEMI.ClientBase.UI
  11. {
  12. public class UIHandler : IHandler
  13. {
  14. public UIHandler()
  15. {
  16. this.acts = new Dictionary<string, Action>();
  17. this.Looper = new MsgPool(500, Do);
  18. }
  19. public void Handle()
  20. {
  21. this.Looper.Run();
  22. }
  23. private void Do(MsgPool looper)
  24. {
  25. lock(lockObj)
  26. {
  27. if (Application.Current == null || Application.Current.Dispatcher == null)
  28. return;
  29. Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate
  30. {
  31. foreach (KeyValuePair<string, Action> kv in this.acts)
  32. {
  33. kv.Value();
  34. }
  35. });
  36. }
  37. }
  38. public void Register(string key, Action act)
  39. {
  40. lock(lockObj)
  41. {
  42. if (!this.acts.ContainsKey(key))
  43. {
  44. this.acts.Add(key, act);
  45. }
  46. }
  47. }
  48. public void UnRegister(string key)
  49. {
  50. lock(lockObj)
  51. {
  52. if (this.acts.ContainsKey(key))
  53. this.acts.Remove(key);
  54. }
  55. }
  56. public MsgPool Looper { get; private set; }
  57. private Dictionary<string, Action> acts;
  58. private static object lockObj = new object();
  59. }
  60. }