当前位置: 首页>>代码示例>>C#>>正文


C# CancellationToken.InternalRegisterWithoutEC方法代码示例

本文整理汇总了C#中System.Threading.CancellationToken.InternalRegisterWithoutEC方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.InternalRegisterWithoutEC方法的具体用法?C# CancellationToken.InternalRegisterWithoutEC怎么用?C# CancellationToken.InternalRegisterWithoutEC使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.CancellationToken的用法示例。


在下文中一共展示了CancellationToken.InternalRegisterWithoutEC方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Wait

        /// <summary>
        /// Blocks the current thread until the current <see cref="ManualResetEventSlim"/> is set, using a
        /// 32-bit signed integer to measure the time interval, while observing a <see
        /// cref="T:System.Threading.CancellationToken"/>.
        /// </summary>
        /// <param name="millisecondsTimeout">The number of milliseconds to wait, or <see
        /// cref="Timeout.Infinite"/>(-1) to wait indefinitely.</param>
        /// <param name="cancellationToken">The <see cref="T:System.Threading.CancellationToken"/> to
        /// observe.</param>
        /// <returns>true if the <see cref="System.Threading.ManualResetEventSlim"/> was set; otherwise,
        /// false.</returns>
        /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="millisecondsTimeout"/> is a
        /// negative number other than -1, which represents an infinite time-out.</exception>
        /// <exception cref="T:System.InvalidOperationException">
        /// The maximum number of waiters has been exceeded.
        /// </exception>
        /// <exception cref="T:System.Threading.OperationCanceledException"><paramref
        /// name="cancellationToken"/> was canceled.</exception>
        public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            ThrowIfDisposed();
            cancellationToken.ThrowIfCancellationRequested(); // an early convenience check

            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }

            if (!IsSet)
            {
                if (millisecondsTimeout == 0)
                {
                    // For 0-timeouts, we just return immediately.
                    return false;
                }


                // We spin briefly before falling back to allocating and/or waiting on a true event.
                uint startTime = 0;
                bool bNeedTimeoutAdjustment = false;
                int realMillisecondsTimeout = millisecondsTimeout; //this will be adjusted if necessary.

                if (millisecondsTimeout != Timeout.Infinite)
                {
                    // We will account for time spent spinning, so that we can decrement it from our
                    // timeout.  In most cases the time spent in this section will be negligible.  But
                    // we can't discount the possibility of our thread being switched out for a lengthy
                    // period of time.  The timeout adjustments only take effect when and if we actually
                    // decide to block in the kernel below.

                    startTime = TimeoutHelper.GetTime();
                    bNeedTimeoutAdjustment = true;
                }

                //spin
                int HOW_MANY_SPIN_BEFORE_YIELD = 10;
                int HOW_MANY_YIELD_EVERY_SLEEP_0 = 5;
                int HOW_MANY_YIELD_EVERY_SLEEP_1 = 20;

                int spinCount = SpinCount;
                for (int i = 0; i < spinCount; i++)
                {
                    if (IsSet)
                    {
                        return true;
                    }

                    else if (i < HOW_MANY_SPIN_BEFORE_YIELD)
                    {
                        if (i == HOW_MANY_SPIN_BEFORE_YIELD / 2)
                        {
                            SpinWait.Yield();
                        }
                        else
                        {
                            SpinWait.Spin(PlatformHelper.ProcessorCount * (4 << i));
                        }
                    }
                    else if (i % HOW_MANY_YIELD_EVERY_SLEEP_1 == 0)
                    {
                        Helpers.Sleep(1);
                    }
                    else if (i % HOW_MANY_YIELD_EVERY_SLEEP_0 == 0)
                    {
                        Helpers.Sleep(0);
                    }
                    else
                    {
                        SpinWait.Yield();
                    }

                    if (i >= 100 && i % 10 == 0) // check the cancellation token if the user passed a very large spin count
                        cancellationToken.ThrowIfCancellationRequested();
                }

                // Now enter the lock and wait.
                EnsureLockObjectCreated();

                // We must register and deregister the token outside of the lock, to avoid deadlocks.
                using (cancellationToken.InternalRegisterWithoutEC(s_cancellationTokenCallback, this))
//.........这里部分代码省略.........
开发者ID:noahfalk,项目名称:corert,代码行数:101,代码来源:ManualResetEventSlim.cs

示例2: CreateLinkedTokenSource

 public static CancellationTokenSource CreateLinkedTokenSource(CancellationToken token1, CancellationToken token2)
 {
     CancellationTokenSource state = new CancellationTokenSource();
     if (token1.CanBeCanceled)
     {
         state.m_linkingRegistrations = new List<CancellationTokenRegistration>();
         state.m_linkingRegistrations.Add(token1.InternalRegisterWithoutEC(s_LinkedTokenCancelDelegate, state));
     }
     if (token2.CanBeCanceled)
     {
         if (state.m_linkingRegistrations == null)
         {
             state.m_linkingRegistrations = new List<CancellationTokenRegistration>();
         }
         state.m_linkingRegistrations.Add(token2.InternalRegisterWithoutEC(s_LinkedTokenCancelDelegate, state));
     }
     return state;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:18,代码来源:CancellationTokenSource.cs

示例3: CreateLinkedTokenSource

 public static CancellationTokenSource CreateLinkedTokenSource(CancellationToken token1, CancellationToken token2)
 {
     CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
     bool canBeCanceled = token2.CanBeCanceled;
     if (token1.CanBeCanceled)
     {
         cancellationTokenSource.m_linkingRegistrations = new CancellationTokenRegistration[canBeCanceled ? 2 : 1];
         cancellationTokenSource.m_linkingRegistrations[0] = token1.InternalRegisterWithoutEC(CancellationTokenSource.s_LinkedTokenCancelDelegate, cancellationTokenSource);
     }
     if (canBeCanceled)
     {
         int num = 1;
         if (cancellationTokenSource.m_linkingRegistrations == null)
         {
             cancellationTokenSource.m_linkingRegistrations = new CancellationTokenRegistration[1];
             num = 0;
         }
         cancellationTokenSource.m_linkingRegistrations[num] = token2.InternalRegisterWithoutEC(CancellationTokenSource.s_LinkedTokenCancelDelegate, cancellationTokenSource);
     }
     return cancellationTokenSource;
 }
开发者ID:rmc00,项目名称:gsf,代码行数:21,代码来源:CancellationTokenSource.cs

示例4: Wait

 public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
 {
     this.ThrowIfDisposed();
     cancellationToken.ThrowIfCancellationRequested();
     if (millisecondsTimeout < -1)
     {
         throw new ArgumentOutOfRangeException("millisecondsTimeout");
     }
     if (!this.IsSet)
     {
         if (millisecondsTimeout == 0)
         {
             return false;
         }
         uint startTime = 0u;
         bool flag = false;
         int num = millisecondsTimeout;
         if (millisecondsTimeout != -1)
         {
             startTime = TimeoutHelper.GetTime();
             flag = true;
         }
         int num2 = 10;
         int num3 = 5;
         int num4 = 20;
         int spinCount = this.SpinCount;
         for (int i = 0; i < spinCount; i++)
         {
             if (this.IsSet)
             {
                 return true;
             }
             if (i < num2)
             {
                 if (i == num2 / 2)
                 {
                     Thread.Sleep(0);
                 }
                 else
                 {
                     Thread.SpinWait(PlatformHelper.ProcessorCount * (4 << i));
                 }
             }
             else
             {
                 if (i % num4 == 0)
                 {
                     Thread.Sleep(1);
                 }
                 else
                 {
                     Thread.Sleep(0);
                     //if (i % num3 == 0)
                     //{
                     //    Thread.Sleep(0);
                     //}
                     //else
                     //{
                     //    Thread.Sleep(0);
                     //}
                 }
             }
             if (i >= 100 && i % 10 == 0)
             {
                 cancellationToken.ThrowIfCancellationRequested();
             }
         }
         this.EnsureLockObjectCreated();
         using (cancellationToken.InternalRegisterWithoutEC(ManualResetEventSlim.s_cancellationTokenCallback, this))
         {
             lock (this.m_lock)
             {
                 while (!this.IsSet)
                 {
                     cancellationToken.ThrowIfCancellationRequested();
                     if (flag)
                     {
                         num = TimeoutHelper.UpdateTimeOut(startTime, millisecondsTimeout);
                         if (num <= 0)
                         {
                             bool result = false;
                             return result;
                         }
                     }
                     this.Waiters++;
                     if (this.IsSet)
                     {
                         this.Waiters--;
                         bool result = true;
                         return result;
                     }
                     try
                     {
                         if (!Monitor.Wait(this.m_lock, num))
                         {
                             bool result = false;
                             return result;
                         }
                     }
                     finally
//.........这里部分代码省略.........
开发者ID:rmc00,项目名称:gsf,代码行数:101,代码来源:ManualResetEventSlim.cs

示例5: CreateLinkedTokenSource

 /// <summary>
 /// Creates the linked token source.
 /// </summary>
 /// <param name="token1">The token1.</param>
 /// <param name="token2">The token2.</param>
 /// <returns></returns>
 public static CancellationTokenSource CreateLinkedTokenSource(CancellationToken token1, CancellationToken token2)
 {
     var state = new CancellationTokenSource();
     var canBeCanceled = token2.CanBeCanceled;
     if (token1.CanBeCanceled)
     {
         state._linkingRegistrations = new CancellationTokenRegistration[canBeCanceled ? 2 : 1];
         state._linkingRegistrations[0] = token1.InternalRegisterWithoutEC(_LinkedTokenCancelDelegate, state);
     }
     if (canBeCanceled)
     {
         var index = 1;
         if (state._linkingRegistrations == null)
         {
             state._linkingRegistrations = new CancellationTokenRegistration[1];
             index = 0;
         }
         state._linkingRegistrations[index] = token2.InternalRegisterWithoutEC(_LinkedTokenCancelDelegate, state);
     }
     return state;
 }
开发者ID:BclEx,项目名称:BclEx-Extend,代码行数:27,代码来源:CancellationTokenSource.cs


注:本文中的System.Threading.CancellationToken.InternalRegisterWithoutEC方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。