|
| 1 | +using System; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using Windows.UI.Core; |
| 4 | + |
| 5 | +namespace Files.Helpers |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// This class provides static methods helper for executing code in UI thread of the main window. |
| 9 | + /// </summary> |
| 10 | + static class DispatcherHelper |
| 11 | + { /// <summary> |
| 12 | + /// This struct represents an awaitable dispatcher. |
| 13 | + /// </summary> |
| 14 | + public struct DispatcherPriorityAwaitable |
| 15 | + { |
| 16 | + private readonly CoreDispatcher dispatcher; |
| 17 | + private readonly CoreDispatcherPriority priority; |
| 18 | + |
| 19 | + internal DispatcherPriorityAwaitable(CoreDispatcher dispatcher, CoreDispatcherPriority priority) |
| 20 | + { |
| 21 | + this.dispatcher = dispatcher; |
| 22 | + this.priority = priority; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Get awaiter of DispatcherPriorityAwaiter |
| 27 | + /// </summary> |
| 28 | + /// <returns>Awaiter of DispatcherPriorityAwaiter</returns> |
| 29 | + public DispatcherPriorityAwaiter GetAwaiter() |
| 30 | + { |
| 31 | + return new DispatcherPriorityAwaiter(this.dispatcher, this.priority); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// This struct represents the awaiter of a dispatcher. |
| 37 | + /// </summary> |
| 38 | + public struct DispatcherPriorityAwaiter : INotifyCompletion |
| 39 | + { |
| 40 | + private readonly CoreDispatcher dispatcher; |
| 41 | + private readonly CoreDispatcherPriority priority; |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets a value indicating whether task has completed |
| 45 | + /// </summary> |
| 46 | + public bool IsCompleted => false; |
| 47 | + |
| 48 | + internal DispatcherPriorityAwaiter(CoreDispatcher dispatcher, CoreDispatcherPriority priority) |
| 49 | + { |
| 50 | + this.dispatcher = dispatcher; |
| 51 | + this.priority = priority; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Get result for this awaiter |
| 56 | + /// </summary> |
| 57 | + public void GetResult() |
| 58 | + { |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Fired once task has complated for notify completion |
| 63 | + /// </summary> |
| 64 | + /// <param name="continuation">Continuation action</param> |
| 65 | + public async void OnCompleted(Action continuation) |
| 66 | + { |
| 67 | + await this.dispatcher.RunAsync(this.priority, new DispatchedHandler(continuation)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Yield and allow UI update during tasks. |
| 73 | + /// </summary> |
| 74 | + /// <param name="dispatcher">Dispatcher of a thread to yield</param> |
| 75 | + /// <param name="priority">Dispatcher execution priority, default is low</param> |
| 76 | + /// <returns>Awaitable dispatcher task</returns> |
| 77 | + public static DispatcherPriorityAwaitable YieldAsync(this CoreDispatcher dispatcher, CoreDispatcherPriority priority = CoreDispatcherPriority.Low) |
| 78 | + { |
| 79 | + return new DispatcherPriorityAwaitable(dispatcher, priority); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments