Skip to content

Instantly share code, notes, and snippets.

@EgorBo
Created November 8, 2017 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EgorBo/7671b87b9edd2d0cb248cef2ea2e995e to your computer and use it in GitHub Desktop.
Save EgorBo/7671b87b9edd2d0cb248cef2ea2e995e to your computer and use it in GitHub Desktop.
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