Created
November 8, 2017 13:28
-
-
Save EgorBo/7671b87b9edd2d0cb248cef2ea2e995e 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
using System; | |
using System.Threading; | |
namespace Foo | |
{ | |
public class Pr | |
{ | |
public static void Main(string[] args) | |
{ | |
var tsc = new TestSynchronizationContext(); | |
SynchronizationContext.SetSynchronizationContext(tsc); | |
var e = new ManualResetEvent(false); | |
tsc.WaitAction = () => e.Set(); | |
tsc.SetWaitNotificationRequired(); | |
Console.WriteLine("WaitOne: " + e.WaitOne(0)); | |
//.NET Core & .NET: true | |
//Mono: false | |
} | |
} | |
class TestSynchronizationContext : SynchronizationContext | |
{ | |
public Action WaitAction { get; set; } | |
public new void SetWaitNotificationRequired() | |
{ | |
base.SetWaitNotificationRequired(); | |
} | |
public override int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) | |
{ | |
// Mono doesn't trigger this method | |
WaitAction?.Invoke(); | |
return base.Wait(waitHandles, waitAll, millisecondsTimeout); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment