Ejemplo n.º 1
0
        // -----------------------------------
        // 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);
        }
Ejemplo n.º 2
0
 /// <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);
     }
 }
Ejemplo n.º 3
0
 public CancellationToken(bool canceled)
 {
     this = default(CancellationToken);
     if (canceled)
     {
         this.m_source = CancellationTokenSource.InternalGetStaticSource(canceled);
     }
 }
Ejemplo n.º 4
0
 public override int GetHashCode()
 {
     if (this.m_source == null)
     {
         return(CancellationTokenSource.InternalGetStaticSource(false).GetHashCode());
     }
     return(this.m_source.GetHashCode());
 }
Ejemplo n.º 5
0
 public CancellationToken(bool canceled)
 {
     this = new CancellationToken();
     if (!canceled)
     {
         return;
     }
     this.m_source = CancellationTokenSource.InternalGetStaticSource(canceled);
 }
Ejemplo n.º 6
0
        /// <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());
        }
Ejemplo n.º 7
0
 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);
 }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
 private void InitializeDefaultSource()
 {
     this.m_source = CancellationTokenSource.InternalGetStaticSource(false);
 }