|
| 1 | + |
| 2 | +using System; |
| 3 | + |
| 4 | +#if WPF || NETSTANDARD2_0 |
| 5 | +using System.Windows; |
| 6 | +using System.Threading.Tasks; |
| 7 | +#endif |
| 8 | + |
| 9 | +#if XF |
| 10 | +using Xamarin.Forms; |
| 11 | +using ZPF.XF; |
| 12 | +#endif |
| 13 | + |
| 14 | +namespace ZPF |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// DoIt ... |
| 18 | + /// |
| 19 | + /// 25/05/17 - ME - Creation ... |
| 20 | + /// 29/05/17 - ME - WPF |
| 21 | + /// 31/05/17 - ME - add: static bool OnBackground(action) |
| 22 | + /// 31/05/17 - ME - WPF: static void Delay(int millisecondsDelay, Action action) |
| 23 | + /// 20/03/18 - ME - WinCE: static void Delay(int millisecondsDelay, Action action) |
| 24 | + /// 30/11/18 - ME - NETCOREAPP2_0 |
| 25 | + /// 07/06/19 - ME - Bugfix: OnMainThread: NETCOREAPP2_0 |
| 26 | + /// |
| 27 | + /// 2017..2019 SAS ZPF |
| 28 | + /// </summary> |
| 29 | + public static class DoIt |
| 30 | + { |
| 31 | + /// <summary> |
| 32 | + /// |
| 33 | + /// </summary> |
| 34 | + /// <param name="action"></param> |
| 35 | + public static void OnBackground(Action action) |
| 36 | + { |
| 37 | + if (action == null) return; |
| 38 | + |
| 39 | +#if XF || WINDOWS_UWP || STD || WPF || WebService || NETCOREAPP2_0 || NETSTANDARD2_0 |
| 40 | + System.Threading.Tasks.Task.Factory.StartNew(() => |
| 41 | + { |
| 42 | + action(); |
| 43 | + }); |
| 44 | +#endif |
| 45 | + |
| 46 | +#if WINCEl |
| 47 | + Delay(1, action); |
| 48 | +#endif |
| 49 | + } |
| 50 | + |
| 51 | +#if WPF || WebService || NETSTANDARD2_0 |
| 52 | + /// <summary> |
| 53 | + /// |
| 54 | + /// </summary> |
| 55 | + /// <param name="action"></param> |
| 56 | + /// <param name="callback"></param> |
| 57 | + public static void OnBackground(Action action, Action<object> callback) |
| 58 | + { |
| 59 | + System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker(); |
| 60 | + worker.RunWorkerCompleted += (object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) => |
| 61 | + { |
| 62 | + callback(sender); |
| 63 | + }; |
| 64 | + worker.DoWork += (object sender, System.ComponentModel.DoWorkEventArgs e) => |
| 65 | + { |
| 66 | + action(); |
| 67 | + }; |
| 68 | + worker.RunWorkerAsync(); |
| 69 | + } |
| 70 | +#endif |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// |
| 74 | + /// </summary> |
| 75 | + /// <param name="action"></param> |
| 76 | + /// <returns></returns> |
| 77 | + public static bool OnBackground(Func<bool> action) |
| 78 | + { |
| 79 | + if (action == null) return false; |
| 80 | + |
| 81 | +#if XF || WINDOWS_UWP || STD || WPF || WebService || NETCOREAPP2_0 || NETSTANDARD2_0 |
| 82 | + System.Threading.Tasks.Task.Factory.StartNew(() => |
| 83 | + { |
| 84 | + return action(); |
| 85 | + }); |
| 86 | +#endif |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | +#if WINDOWS_UWP |
| 92 | + public static async void OnMainThread(Action action) |
| 93 | + { |
| 94 | + await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( |
| 95 | + Windows.UI.Core.CoreDispatcherPriority.Normal, () => |
| 96 | + { |
| 97 | + action(); |
| 98 | + }); |
| 99 | + } |
| 100 | +#else |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// |
| 104 | + /// </summary> |
| 105 | + /// <param name="action"></param> |
| 106 | + public static void OnMainThread(Action action) |
| 107 | + { |
| 108 | + if (action == null) return; |
| 109 | + |
| 110 | +#if XF |
| 111 | + Device.BeginInvokeOnMainThread(() => |
| 112 | + { |
| 113 | + action(); |
| 114 | + }); |
| 115 | + |
| 116 | + return; |
| 117 | +#endif |
| 118 | + |
| 119 | +#if WPF |
| 120 | + Application.Current.Dispatcher.Invoke(new Action(() => |
| 121 | + { |
| 122 | + action(); |
| 123 | + })); |
| 124 | + |
| 125 | + return; |
| 126 | +#endif |
| 127 | + |
| 128 | +#if NETSTANDARD2_0 |
| 129 | + //System.Threading.Tasks.D Dispatcher. |
| 130 | + //Invoke(new Action(() => |
| 131 | + //{ |
| 132 | + action(); |
| 133 | + //})); |
| 134 | +#endif |
| 135 | + |
| 136 | + } |
| 137 | +#endif |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Creates a task that completes after a time delay. |
| 141 | + /// </summary> |
| 142 | + /// <param name="millisecondsDelay">The number of milliseconds to wait before completing the returned task, or -1 to wait indefinitely.</param> |
| 143 | + /// <param name="action"></param> |
| 144 | + public static void Delay(int millisecondsDelay, Action action) |
| 145 | + { |
| 146 | + if (action == null) return; |
| 147 | + if (millisecondsDelay < 1) return; |
| 148 | + |
| 149 | +#if XF|| WINDOWS_UWP || STD || WPF || WebService || NETCOREAPP2_0 || NETSTANDARD2_0 |
| 150 | + var task = System.Threading.Tasks.Task.Delay(millisecondsDelay).ContinueWith(t => action()); |
| 151 | +#endif |
| 152 | + |
| 153 | +#if WINCE |
| 154 | + System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(); |
| 155 | + t.Interval = millisecondsDelay; |
| 156 | + t.Tick += (object _sender, EventArgs _e) => |
| 157 | + { |
| 158 | + t.Enabled = false; |
| 159 | + |
| 160 | + action(); |
| 161 | + }; |
| 162 | + t.Enabled = true; |
| 163 | +#endif |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary> |
| 167 | + /// ???? Creates a task that completes after a time delay. ???? |
| 168 | + /// </summary> |
| 169 | + /// <param name="millisecondsDelay">The number of milliseconds to wait before completing the returned task, or -1 to wait indefinitely.</param> |
| 170 | + /// <param name="action"></param> |
| 171 | +#if !WINDOWS_UWP |
| 172 | +#if XF || WINDOWS_UWP || STD |
| 173 | +#if NETSTANDARD2_0 |
| 174 | + |
| 175 | + public static System.Timers.Timer Timeout(System.Timers.Timer _Timer, int millisecondsDelay, Action action) |
| 176 | + { |
| 177 | + if (action == null) return null; |
| 178 | + if (millisecondsDelay < 1) return null; |
| 179 | + |
| 180 | + //ToDo ¤ !!! timer |
| 181 | + if (_Timer != null) |
| 182 | + { |
| 183 | + _Timer.Stop(); |
| 184 | + }; |
| 185 | + |
| 186 | + _Timer = new System.Timers.Timer(millisecondsDelay); |
| 187 | + _Timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) => |
| 188 | +#else |
| 189 | + |
| 190 | + public static PCLTimer Timeout(PCLTimer _Timer, int millisecondsDelay, Action action) |
| 191 | + { |
| 192 | + if (_Timer != null) |
| 193 | + { |
| 194 | + _Timer.Stop(); |
| 195 | + }; |
| 196 | + |
| 197 | + _Timer = new PCLTimer(); |
| 198 | + _Timer.Interval = millisecondsDelay; |
| 199 | + _Timer.Elapsed += (object sender, EventArgs e) => |
| 200 | +#endif |
| 201 | + { |
| 202 | + _Timer.Stop(); |
| 203 | + action(); |
| 204 | + }; |
| 205 | + |
| 206 | + _Timer.Start(); |
| 207 | + |
| 208 | + return _Timer; |
| 209 | + } |
| 210 | +#endif |
| 211 | +#endif |
| 212 | + } |
| 213 | +} |
0 commit comments