hytos / ID2.Manager / ID2.Manager.Compare / CustomSynchronizationContext.cs @ 1620ca9c
이력 | 보기 | 이력해설 | 다운로드 (2.57 KB)
1 | 13a36357 | humkyung | using System; |
---|---|---|---|
2 | using System.ComponentModel; |
||
3 | using System.Runtime.InteropServices; |
||
4 | using System.Security; |
||
5 | using System.Threading; |
||
6 | using System.Windows.Forms; |
||
7 | |||
8 | /// <summary> |
||
9 | /// This class helps to avoid some synchornization issues between WinForms UI and Eyeshot parallel execution. More details here: https://stackoverflow.com/questions/35535580/why-does-parallel-for-execute-the-winforms-message-pump-and-how-to-prevent-it |
||
10 | /// The problem (huge delay) was reproducible switching between two tabs of the EyeshotDemo that build a BRep (for example Bracket and Flange) and moving the mouse cursor very fast on the control surface just after tab switch. |
||
11 | /// A similar problem (flickering) was present in the MarchingCubes sample. |
||
12 | /// </summary> |
||
13 | public class CustomSynchronizationContext : SynchronizationContext |
||
14 | { |
||
15 | public static void Install() |
||
16 | { |
||
17 | var currentContext = Current; |
||
18 | if (currentContext is CustomSynchronizationContext) return; |
||
19 | WindowsFormsSynchronizationContext.AutoInstall = false; |
||
20 | SetSynchronizationContext(new CustomSynchronizationContext(currentContext)); |
||
21 | } |
||
22 | |||
23 | public static void Uninstall() |
||
24 | { |
||
25 | var currentContext = Current as CustomSynchronizationContext; |
||
26 | if (currentContext == null) return; |
||
27 | SetSynchronizationContext(currentContext.baseContext); |
||
28 | } |
||
29 | |||
30 | private WindowsFormsSynchronizationContext baseContext; |
||
31 | |||
32 | private CustomSynchronizationContext(SynchronizationContext currentContext) |
||
33 | { |
||
34 | baseContext = currentContext as WindowsFormsSynchronizationContext ?? new WindowsFormsSynchronizationContext(); |
||
35 | SetWaitNotificationRequired(); |
||
36 | } |
||
37 | |||
38 | public override SynchronizationContext CreateCopy() { return this; } |
||
39 | public override void Post(SendOrPostCallback d, object state) { baseContext.Post(d, state); } |
||
40 | public override void Send(SendOrPostCallback d, object state) { baseContext.Send(d, state); } |
||
41 | public override void OperationStarted() { baseContext.OperationStarted(); } |
||
42 | public override void OperationCompleted() { baseContext.OperationCompleted(); } |
||
43 | |||
44 | public override int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) |
||
45 | { |
||
46 | int result = WaitForMultipleObjectsEx(waitHandles.Length, waitHandles, waitAll, millisecondsTimeout, false); |
||
47 | if (result == -1) throw new Win32Exception(); |
||
48 | return result; |
||
49 | } |
||
50 | |||
51 | [SuppressUnmanagedCodeSecurity] |
||
52 | [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
||
53 | private static extern int WaitForMultipleObjectsEx(int nCount, IntPtr[] pHandles, bool bWaitAll, int dwMilliseconds, bool bAlertable); |
||
54 | } |