UIHandler.cs 1.2 KB

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