-
-
Save ilexp/3c9135e051b2dd95b3ce24ec8d39e422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomSynchronizationContext : SynchronizationContext | |
{ | |
public static void Install() | |
{ | |
var currentContext = Current; | |
if (currentContext is CustomSynchronizationContext) return; | |
WindowsFormsSynchronizationContext.AutoInstall = false; | |
SetSynchronizationContext(new CustomSynchronizationContext(currentContext)); | |
} | |
public static void Uninstall() | |
{ | |
var currentContext = Current as CustomSynchronizationContext; | |
if (currentContext == null) return; | |
SetSynchronizationContext(currentContext.baseContext); | |
} | |
private WindowsFormsSynchronizationContext baseContext; | |
private CustomSynchronizationContext(SynchronizationContext currentContext) | |
{ | |
baseContext = currentContext as WindowsFormsSynchronizationContext ?? new WindowsFormsSynchronizationContext(); | |
SetWaitNotificationRequired(); | |
} | |
public override SynchronizationContext CreateCopy() { return this; } | |
public override void Post(SendOrPostCallback d, object state) { baseContext.Post(d, state); } | |
public override void Send(SendOrPostCallback d, object state) { baseContext.Send(d, state); } | |
public override void OperationStarted() { baseContext.OperationStarted(); } | |
public override void OperationCompleted() { baseContext.OperationCompleted(); } | |
public override int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) | |
{ | |
int result = WaitForMultipleObjectsEx(waitHandles.Length, waitHandles, waitAll, millisecondsTimeout, false); | |
if (result == -1) throw new Exception(); | |
return result; | |
} | |
[SuppressUnmanagedCodeSecurity] | |
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] | |
private static extern int WaitForMultipleObjectsEx(int nCount, IntPtr[] pHandles, bool bWaitAll, int dwMilliseconds, bool bAlertable); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment