|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +namespace Dispatch |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// SerialQueue which takes work to be executed serially. |
| 10 | + /// Work is executed when calling TryDoWork |
| 11 | + /// </summary> |
| 12 | + public class MainQueue : IDispatchQueue |
| 13 | + { |
| 14 | + #region Internal Declarations |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Class used to intercept the Dispatch calls from the internal Serial Queue |
| 18 | + /// </summary> |
| 19 | + private class WorkHolder : IDispatcher |
| 20 | + { |
| 21 | + WaitCallback? mCurrentWork = null; |
| 22 | + object? mCurrentContext; |
| 23 | + |
| 24 | + public void QueueWorkItem(WaitCallback work, object? context) |
| 25 | + { |
| 26 | + if (mCurrentWork != null) |
| 27 | + { |
| 28 | + throw new InvalidOperationException("Queueing work but previous work is not complete"); |
| 29 | + } |
| 30 | + |
| 31 | + mCurrentWork = work; |
| 32 | + mCurrentContext = context; |
| 33 | + } |
| 34 | + |
| 35 | + public bool TryDoWork() |
| 36 | + { |
| 37 | + if (mCurrentWork == null) |
| 38 | + { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + // make a copy because executing the work will likely cause QueueWorkItem to be called |
| 43 | + WaitCallback workCopy = mCurrentWork; |
| 44 | + mCurrentWork = null; |
| 45 | + |
| 46 | + workCopy.Invoke(mCurrentContext); |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + #endregion |
| 53 | + |
| 54 | + |
| 55 | + #region Member Variables |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// SerialQueue which will hold all our work to do |
| 59 | + /// </summary> |
| 60 | + private readonly SerialQueue mQueue; |
| 61 | + |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// WorkHolder that will intercept the Dispatch calls from mQueue |
| 65 | + /// </summary> |
| 66 | + private readonly WorkHolder mWorkHolder = new WorkHolder(); |
| 67 | + |
| 68 | + #endregion |
| 69 | + |
| 70 | + |
| 71 | + #region Constructors |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Default constructor for MainQueue |
| 75 | + /// </summary> |
| 76 | + public MainQueue() |
| 77 | + { |
| 78 | + mQueue = new SerialQueue(mWorkHolder); |
| 79 | + } |
| 80 | + |
| 81 | + #endregion |
| 82 | + |
| 83 | + |
| 84 | + #region Public Methods |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Will dispatch the work delegate to this queue once at least the specified amount of time has passed |
| 88 | + /// </summary> |
| 89 | + /// <param name="when">Amount of time to wait before submitting the work to this queue</param> |
| 90 | + /// <param name="context">User data to pass to the work delegate</param> |
| 91 | + /// <param name="work">Delegate which will perform the work. Must not be null</param> |
| 92 | + public void DispatchAfter(TimeSpan when, object? context, WaitCallback work) |
| 93 | + { |
| 94 | + mQueue.DispatchAfter(when, context, work); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Will dispatch the work delegate to this queue after the current time has passed the specified date |
| 100 | + /// </summary> |
| 101 | + /// <param name="when">Date after which the work will be submitted to this queue</param> |
| 102 | + /// <param name="context">User data to pass to the work delegate</param> |
| 103 | + /// <param name="work">Delegate which will perform the work. Must not be null</param> |
| 104 | + public void DispatchAfter(DateTime when, object? context, WaitCallback work) |
| 105 | + { |
| 106 | + mQueue.DispatchAfter(when, context, work); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Will dispatch the work delegate to the thread pool once all the previously dispatched work is completed |
| 112 | + /// </summary> |
| 113 | + /// <param name="context">User data to pass to the work delegate</param> |
| 114 | + /// <param name="work">Delegate which will perform the work. Must not be null</param> |
| 115 | + public void DispatchAsync(object? context, WaitCallback work) |
| 116 | + { |
| 117 | + mQueue.DispatchAsync(context, work); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Will attempt to dequeue and execute work which has been queued |
| 123 | + /// </summary> |
| 124 | + /// <returns>true if work has been dequeued and done, otherwise false</returns> |
| 125 | + public bool TryDoWork() |
| 126 | + { |
| 127 | + return mWorkHolder.TryDoWork(); |
| 128 | + } |
| 129 | + |
| 130 | + #endregion |
| 131 | + } |
| 132 | +} |
0 commit comments