|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace Resgrid.Providers.ApiClient |
| 7 | +{ |
| 8 | + public static class AsyncHelpers |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Execute's an async Task<T> method which has a void return value synchronously |
| 12 | + /// </summary> |
| 13 | + /// <param name="task">Task<T> method to execute</param> |
| 14 | + public static void RunSync(Func<Task> task) |
| 15 | + { |
| 16 | + var oldContext = SynchronizationContext.Current; |
| 17 | + var synch = new ExclusiveSynchronizationContext(); |
| 18 | + SynchronizationContext.SetSynchronizationContext(synch); |
| 19 | + synch.Post(async _ => |
| 20 | + { |
| 21 | + try |
| 22 | + { |
| 23 | + await task(); |
| 24 | + } |
| 25 | + catch (Exception e) |
| 26 | + { |
| 27 | + synch.InnerException = e; |
| 28 | + throw; |
| 29 | + } |
| 30 | + finally |
| 31 | + { |
| 32 | + synch.EndMessageLoop(); |
| 33 | + } |
| 34 | + }, null); |
| 35 | + synch.BeginMessageLoop(); |
| 36 | + |
| 37 | + SynchronizationContext.SetSynchronizationContext(oldContext); |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Execute's an async Task<T> method which has a T return type synchronously |
| 42 | + /// </summary> |
| 43 | + /// <typeparam name="T">Return Type</typeparam> |
| 44 | + /// <param name="task">Task<T> method to execute</param> |
| 45 | + /// <returns></returns> |
| 46 | + public static T RunSync<T>(Func<Task<T>> task) |
| 47 | + { |
| 48 | + var oldContext = SynchronizationContext.Current; |
| 49 | + var synch = new ExclusiveSynchronizationContext(); |
| 50 | + SynchronizationContext.SetSynchronizationContext(synch); |
| 51 | + T ret = default(T); |
| 52 | + synch.Post(async _ => |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + ret = await task(); |
| 57 | + } |
| 58 | + catch (Exception e) |
| 59 | + { |
| 60 | + synch.InnerException = e; |
| 61 | + throw; |
| 62 | + } |
| 63 | + finally |
| 64 | + { |
| 65 | + synch.EndMessageLoop(); |
| 66 | + } |
| 67 | + }, null); |
| 68 | + synch.BeginMessageLoop(); |
| 69 | + SynchronizationContext.SetSynchronizationContext(oldContext); |
| 70 | + return ret; |
| 71 | + } |
| 72 | + |
| 73 | + private class ExclusiveSynchronizationContext : SynchronizationContext |
| 74 | + { |
| 75 | + private bool done; |
| 76 | + public Exception InnerException { get; set; } |
| 77 | + readonly AutoResetEvent workItemsWaiting = new AutoResetEvent(false); |
| 78 | + readonly Queue<Tuple<SendOrPostCallback, object>> items = |
| 79 | + new Queue<Tuple<SendOrPostCallback, object>>(); |
| 80 | + |
| 81 | + public override void Send(SendOrPostCallback d, object state) |
| 82 | + { |
| 83 | + throw new NotSupportedException("We cannot send to our same thread"); |
| 84 | + } |
| 85 | + |
| 86 | + public override void Post(SendOrPostCallback d, object state) |
| 87 | + { |
| 88 | + lock (items) |
| 89 | + { |
| 90 | + items.Enqueue(Tuple.Create(d, state)); |
| 91 | + } |
| 92 | + workItemsWaiting.Set(); |
| 93 | + } |
| 94 | + |
| 95 | + public void EndMessageLoop() |
| 96 | + { |
| 97 | + Post(_ => done = true, null); |
| 98 | + } |
| 99 | + |
| 100 | + public void BeginMessageLoop() |
| 101 | + { |
| 102 | + while (!done) |
| 103 | + { |
| 104 | + Tuple<SendOrPostCallback, object> task = null; |
| 105 | + lock (items) |
| 106 | + { |
| 107 | + if (items.Count > 0) |
| 108 | + { |
| 109 | + task = items.Dequeue(); |
| 110 | + } |
| 111 | + } |
| 112 | + if (task != null) |
| 113 | + { |
| 114 | + task.Item1(task.Item2); |
| 115 | + if (InnerException != null) // the method threw an exeption |
| 116 | + { |
| 117 | + throw new AggregateException("AsyncHelpers.Run method threw an exception.", InnerException); |
| 118 | + } |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + workItemsWaiting.WaitOne(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public override SynchronizationContext CreateCopy() |
| 128 | + { |
| 129 | + return this; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments