SynchronizationContext.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using Thread = Internal.Runtime.Augments.RuntimeThread;
  5. namespace System.Threading
  6. {
  7. public partial class SynchronizationContext
  8. {
  9. private bool _requireWaitNotification;
  10. public SynchronizationContext()
  11. {
  12. }
  13. #if !FEATURE_APPX && !ENABLE_WINRT
  14. public static SynchronizationContext Current => Thread.CurrentThread.SynchronizationContext;
  15. #endif
  16. protected void SetWaitNotificationRequired() => _requireWaitNotification = true;
  17. public bool IsWaitNotificationRequired() => _requireWaitNotification;
  18. public virtual void Send(SendOrPostCallback d, object state) => d(state);
  19. public virtual void Post(SendOrPostCallback d, object state) => ThreadPool.QueueUserWorkItem(s => s.d(s.state), (d, state), preferLocal: false);
  20. /// <summary>
  21. /// Optional override for subclasses, for responding to notification that operation is starting.
  22. /// </summary>
  23. public virtual void OperationStarted()
  24. {
  25. }
  26. /// <summary>
  27. /// Optional override for subclasses, for responding to notification that operation has completed.
  28. /// </summary>
  29. public virtual void OperationCompleted()
  30. {
  31. }
  32. [CLSCompliant(false)]
  33. public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
  34. {
  35. return WaitHelper(waitHandles, waitAll, millisecondsTimeout);
  36. }
  37. [CLSCompliant(false)]
  38. protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout)
  39. {
  40. if (waitHandles == null)
  41. {
  42. throw new ArgumentNullException(nameof(waitHandles));
  43. }
  44. return WaitHandle.WaitMultipleIgnoringSyncContext(waitHandles, waitAll, millisecondsTimeout);
  45. }
  46. public static void SetSynchronizationContext(SynchronizationContext syncContext) => Thread.CurrentThread.SynchronizationContext = syncContext;
  47. public virtual SynchronizationContext CreateCopy() => new SynchronizationContext();
  48. }
  49. }