|
| 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 | + |
| 5 | +using Thread = Internal.Runtime.Augments.RuntimeThread; |
| 6 | + |
| 7 | +namespace System.Threading |
| 8 | +{ |
| 9 | + public partial class SynchronizationContext |
| 10 | + { |
| 11 | + private bool _requireWaitNotification; |
| 12 | + |
| 13 | + public SynchronizationContext() |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | +#if !FEATURE_APPX && !ENABLE_WINRT |
| 18 | + public static SynchronizationContext Current => Thread.CurrentThread.SynchronizationContext; |
| 19 | +#endif |
| 20 | + |
| 21 | + protected void SetWaitNotificationRequired() => _requireWaitNotification = true; |
| 22 | + |
| 23 | + public bool IsWaitNotificationRequired() => _requireWaitNotification; |
| 24 | + |
| 25 | + public virtual void Send(SendOrPostCallback d, object state) => d(state); |
| 26 | + |
| 27 | + public virtual void Post(SendOrPostCallback d, object state) => ThreadPool.QueueUserWorkItem(s => s.d(s.state), (d, state), preferLocal: false); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Optional override for subclasses, for responding to notification that operation is starting. |
| 31 | + /// </summary> |
| 32 | + public virtual void OperationStarted() |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Optional override for subclasses, for responding to notification that operation has completed. |
| 38 | + /// </summary> |
| 39 | + public virtual void OperationCompleted() |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + [CLSCompliant(false)] |
| 44 | + public virtual int Wait(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) |
| 45 | + { |
| 46 | + return WaitHelper(waitHandles, waitAll, millisecondsTimeout); |
| 47 | + } |
| 48 | + |
| 49 | + [CLSCompliant(false)] |
| 50 | + protected static int WaitHelper(IntPtr[] waitHandles, bool waitAll, int millisecondsTimeout) |
| 51 | + { |
| 52 | + if (waitHandles == null) |
| 53 | + { |
| 54 | + throw new ArgumentNullException(nameof(waitHandles)); |
| 55 | + } |
| 56 | + |
| 57 | + return WaitHandle.WaitMultipleIgnoringSyncContext(waitHandles, waitAll, millisecondsTimeout); |
| 58 | + } |
| 59 | + |
| 60 | + public static void SetSynchronizationContext(SynchronizationContext syncContext) => Thread.CurrentThread.SynchronizationContext = syncContext; |
| 61 | + |
| 62 | + public virtual SynchronizationContext CreateCopy() => new SynchronizationContext(); |
| 63 | + } |
| 64 | +} |
0 commit comments