ThreadCallBackDelegate.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace PackingPress.Common
  9. {
  10. public class UIHelper : Application
  11. {
  12. //刷新界面
  13. private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
  14. public static void DoEvents()
  15. {
  16. DispatcherFrame nestedFrame = new DispatcherFrame();
  17. DispatcherOperation exitOperation =
  18. Dispatcher.CurrentDispatcher.BeginInvoke(
  19. DispatcherPriority.Background,
  20. exitFrameCallback, nestedFrame);
  21. Dispatcher.PushFrame(nestedFrame);
  22. if (exitOperation.Status != DispatcherOperationStatus.Completed)
  23. {
  24. exitOperation.Abort();
  25. }
  26. }
  27. private static object ExitFrame(object state)
  28. {
  29. DispatcherFrame frame = state as DispatcherFrame;
  30. frame.Continue = false;
  31. return null;
  32. }
  33. }
  34. public class UIThreadHelper<T,O> where T : class where O : class
  35. {
  36. Window _window;
  37. Dictionary<string, Func<T,O>> _delegateDic = new Dictionary<string, Func<T,O>>();
  38. public UIThreadHelper(Window window)
  39. {
  40. _window = window;
  41. }
  42. public void AddDelegate(string key, Func<T,O> delegateHandle)
  43. {
  44. _delegateDic.Add(key, delegateHandle);
  45. }
  46. public void AddDelegate(Func<T,O> delegateHandle)
  47. {
  48. _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
  49. }
  50. public O RunDelegate(string key,params object[] args)
  51. {
  52. if (_delegateDic.Keys.Contains(key))
  53. {
  54. if (args.Length > 0)
  55. {
  56. return _window.Dispatcher.Invoke(_delegateDic[key], args) as O;
  57. }
  58. else
  59. {
  60. return _window.Dispatcher.Invoke(_delegateDic[key],"") as O;
  61. }
  62. }
  63. return default(O);
  64. }
  65. }
  66. public class UIThreadHelper<T> where T : class
  67. {
  68. Window _window;
  69. Dictionary<string, Action<T>> _delegateDic = new Dictionary<string, Action<T>>();
  70. public UIThreadHelper(Window window)
  71. {
  72. _window = window;
  73. }
  74. public void AddDelegate(string key, Action<T> delegateHandle)
  75. {
  76. _delegateDic.Add(key, delegateHandle);
  77. }
  78. public void AddDelegate(Action<T> delegateHandle)
  79. {
  80. _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
  81. }
  82. public void RunDelegate(string key, params object[] args)
  83. {
  84. if (_delegateDic.Keys.Contains(key))
  85. {
  86. if (args.Length > 0)
  87. {
  88. _window.Dispatcher.Invoke(_delegateDic[key], args);
  89. }
  90. else
  91. {
  92. _window.Dispatcher.Invoke(_delegateDic[key], "");
  93. }
  94. }
  95. }
  96. }
  97. public class UIThreadHelper
  98. {
  99. Window _window;
  100. Dictionary<string, Action> _delegateDic = new Dictionary<string, Action>();
  101. public UIThreadHelper(Window window)
  102. {
  103. _window = window;
  104. }
  105. public void AddDelegate(string key, Action delegateHandle)
  106. {
  107. _delegateDic.Add(key, delegateHandle);
  108. }
  109. public void AddDelegate(Action delegateHandle)
  110. {
  111. _delegateDic.Add(delegateHandle.Method.Name, delegateHandle);
  112. }
  113. public void RunDelegate(string key, params object[] args)
  114. {
  115. if (_delegateDic.Keys.Contains(key))
  116. {
  117. _window.Dispatcher.Invoke(_delegateDic[key]);
  118. }
  119. }
  120. }
  121. }