// ----------------------------------- // Private helpers private void InitializeDefaultSource() { // Lazy is slower, and although multiple threads may try and set m_source repeatedly, the race is benign. // Alternative: LazyInititalizer.EnsureInitialized(ref m_source, ()=>CancellationTokenSource.InternalGetStaticSource(false)); m_source = CancellationTokenSource.InternalGetStaticSource(false); }
/// <summary> /// Initializes the <see cref="T:System.Threading.CancellationToken">CancellationToken</see>. /// </summary> /// <param name="canceled"> /// The canceled state for the token. /// </param> /// <remarks> /// Tokens created with this constructor will remain in the canceled state specified /// by the <paramref name="canceled"/> parameter. If <paramref name="canceled"/> is false, /// both <see cref="CanBeCanceled"/> and <see cref="IsCancellationRequested"/> will be false. /// If <paramref name="canceled"/> is true, /// both <see cref="CanBeCanceled"/> and <see cref="IsCancellationRequested"/> will be true. /// </remarks> public CancellationToken(bool canceled) : this() { if (canceled) { m_source = CancellationTokenSource.InternalGetStaticSource(canceled); } }
public CancellationToken(bool canceled) { this = default(CancellationToken); if (canceled) { this.m_source = CancellationTokenSource.InternalGetStaticSource(canceled); } }
public override int GetHashCode() { if (this.m_source == null) { return(CancellationTokenSource.InternalGetStaticSource(false).GetHashCode()); } return(this.m_source.GetHashCode()); }
public CancellationToken(bool canceled) { this = new CancellationToken(); if (!canceled) { return; } this.m_source = CancellationTokenSource.InternalGetStaticSource(canceled); }
/// <summary> /// Serves as a hash function for a <see cref="T:System.Threading.CancellationToken">CancellationToken</see>. /// </summary> /// <returns>A hash code for the current <see cref="T:System.Threading.CancellationToken">CancellationToken</see> instance.</returns> public override Int32 GetHashCode() { if (m_source == null) { // link to the common source so that we have a source to interrogate. return(CancellationTokenSource.InternalGetStaticSource(false).GetHashCode()); } return(m_source.GetHashCode()); }
public bool Equals(CancellationToken other) { if (this.m_source == null && other.m_source == null) { return(true); } if (this.m_source == null) { return(other.m_source == CancellationTokenSource.InternalGetStaticSource(false)); } if (other.m_source == null) { return(this.m_source == CancellationTokenSource.InternalGetStaticSource(false)); } return(this.m_source == other.m_source); }
/// <summary> /// Determines whether the current <see cref="T:System.Threading.CancellationToken">CancellationToken</see> instance is equal to the /// specified token. /// </summary> /// <param name="other">The other <see cref="T:System.Threading.CancellationToken">CancellationToken</see> to which to compare this /// instance.</param> /// <returns>True if the instances are equal; otherwise, false. Two tokens are equal if they are associated /// with the same <see cref="T:System.Threading.CancellationTokenSource">CancellationTokenSource</see> or if they were both constructed /// from public CancellationToken constructors and their <see cref="IsCancellationRequested"/> values are equal.</returns> public bool Equals(CancellationToken other) { //if both sources are null, then both tokens represent the Empty token. if (m_source == null && other.m_source == null) { return(true); } // one is null but other has inflated the default source // these are only equal if the inflated one is the staticSource(false) if (m_source == null) { return(other.m_source == CancellationTokenSource.InternalGetStaticSource(false)); } if (other.m_source == null) { return(m_source == CancellationTokenSource.InternalGetStaticSource(false)); } // general case, we check if the sources are identical return(m_source == other.m_source); }
private void InitializeDefaultSource() { this.m_source = CancellationTokenSource.InternalGetStaticSource(false); }