|
1 | 1 | using System;
|
| 2 | +using System.Runtime.InteropServices; |
2 | 3 | using System.Threading;
|
3 | 4 | using System.Threading.Tasks;
|
4 | 5 |
|
5 | 6 | namespace Rubberduck.Parsing.UIContext
|
6 | 7 | {
|
7 | 8 | public static class UiDispatcher
|
8 | 9 | {
|
| 10 | + static UiDispatcher() |
| 11 | + { |
| 12 | + _version = DllVersion.Unknown; |
| 13 | + } |
| 14 | + |
9 | 15 | // thanks to Pellared on http://stackoverflow.com/a/12909070/1188513
|
10 | 16 |
|
11 | 17 | private static SynchronizationContext UiContext { get; set; }
|
@@ -113,5 +119,69 @@ public static void Shutdown()
|
113 | 119 | // Dispatcher.CurrentDispatcher.InvokeShutdown();
|
114 | 120 | //});
|
115 | 121 | }
|
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Used to pump any pending COM messages. This should be used only as a part of |
| 125 | + /// synchronizing or to effect a block until all other threads has finished with |
| 126 | + /// their pending COM calls. |
| 127 | + /// </summary> |
| 128 | + public static void DoEvents() |
| 129 | + { |
| 130 | + Invoke(() => ExecuteDoEvents()); |
| 131 | + } |
| 132 | + |
| 133 | + private static int ExecuteDoEvents() |
| 134 | + { |
| 135 | + switch (_version) |
| 136 | + { |
| 137 | + case DllVersion.Vbe7: |
| 138 | + return rtcDoEvents7(); |
| 139 | + case DllVersion.Vbe6: |
| 140 | + return rtcDoEvents6(); |
| 141 | + default: |
| 142 | + return DetermineVersionAndExecute(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private enum DllVersion |
| 147 | + { |
| 148 | + Unknown, |
| 149 | + Vbe6, |
| 150 | + Vbe7 |
| 151 | + } |
| 152 | + |
| 153 | + private static DllVersion _version; |
| 154 | + |
| 155 | + [DllImport("vbe6.dll", EntryPoint = "rtcDoEvents")] |
| 156 | + private static extern int rtcDoEvents6(); |
| 157 | + |
| 158 | + [DllImport("vbe7.dll", EntryPoint = "rtcDoEvents")] |
| 159 | + private static extern int rtcDoEvents7(); |
| 160 | + |
| 161 | + private static int DetermineVersionAndExecute() |
| 162 | + { |
| 163 | + int result; |
| 164 | + try |
| 165 | + |
| 166 | + { |
| 167 | + result = rtcDoEvents7(); |
| 168 | + _version = DllVersion.Vbe7; |
| 169 | + } |
| 170 | + catch |
| 171 | + { |
| 172 | + try |
| 173 | + { |
| 174 | + result = rtcDoEvents6(); |
| 175 | + _version = DllVersion.Vbe6; |
| 176 | + } |
| 177 | + catch |
| 178 | + { |
| 179 | + // we shouldn't be here.... |
| 180 | + throw new InvalidOperationException("Cannot execute DoEvents; the VBE dll could not be located."); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + return result; |
| 185 | + } |
116 | 186 | }
|
117 | 187 | }
|
0 commit comments