private CancellationTokenRegistration Register(Action<Object> callback, Object state, bool useSynchronizationContext, bool useExecutionContext) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; if (callback == null) throw new ArgumentNullException("callback"); if (CanBeCanceled == false) { return new CancellationTokenRegistration(); // nothing to do for tokens than can never reach the canceled state. Give them a dummy registration. } // Capture sync/execution contexts if required. // Note: Only capture sync/execution contexts if IsCancellationRequested = false // as we know that if it is true that the callback will just be called synchronously. SynchronizationContext capturedSyncContext = null; ExecutionContext capturedExecutionContext = null; if (!IsCancellationRequested) { if (useSynchronizationContext) capturedSyncContext = SynchronizationContext.Current; if (useExecutionContext) capturedExecutionContext = ExecutionContext.Capture( ref stackMark, ExecutionContext.CaptureOptions.OptimizeDefaultCase); // ideally we'd also use IgnoreSyncCtx, but that could break compat } // Register the callback with the source. return m_source.InternalRegister(callback, state, capturedSyncContext, capturedExecutionContext); }
public CancellationTokenRegistration Register(Action <Object> callback, Object state, bool useSynchronizationContext, bool useExecutionContext) { if (callback == null) { throw new ArgumentNullException(nameof(callback)); } if (CanBeCanceled == false) { return(new CancellationTokenRegistration()); // nothing to do for tokens than can never reach the canceled state. Give them a dummy registration. } // Capture sync/execution contexts if required. // Note: Only capture sync/execution contexts if IsCancellationRequested = false // as we know that if it is true that the callback will just be called synchronously. SynchronizationContext capturedSyncContext = null; ExecutionContext capturedExecutionContext = null; if (!IsCancellationRequested) { if (useSynchronizationContext) { capturedSyncContext = SynchronizationContext.Current; } if (useExecutionContext) { capturedExecutionContext = ExecutionContext.Capture(); } } // Register the callback with the source. return(m_source.InternalRegister(callback, state, capturedSyncContext, capturedExecutionContext)); }
private CancellationTokenRegistration PrivateRegister(Action <Object> callback, Object state) { if (callback == null) { throw new ArgumentNullException("callback"); } if (CanBeCanceled == false) { return(new CancellationTokenRegistration()); // nothing to do for tokens than can never reach the canceled state. Give them a dummy registration. } // Register the callback with the source. return(m_source.InternalRegister(callback, state, null, null)); }