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


C# TimeSpan.Duration方法代码示例

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


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

示例1: RateGate

            /// <summary>
            /// Initializes a <see cref="RateGate"/> with a rate of <paramref name="occurrences"/> 
            /// per <paramref name="timeUnit"/>.
            /// </summary>
            /// <param name="occurrences">Number of occurrences allowed per unit of time.</param>
            /// <param name="timeUnit">Length of the time unit.</param>
            /// <exception cref="ArgumentOutOfRangeException">
            /// If <paramref name="occurrences"/> or <paramref name="timeUnit"/> is negative.
            /// </exception>
            public RateGate(int occurrences, TimeSpan timeUnit)
            {
                // Check the arguments.
                if (occurrences <= 0)
                    throw new ArgumentOutOfRangeException("occurrences", "Number of occurrences must be a positive integer");
                if (timeUnit != timeUnit.Duration())
                    throw new ArgumentOutOfRangeException("timeUnit", "Time unit must be a positive span of time");
                if (timeUnit >= TimeSpan.FromMilliseconds(UInt32.MaxValue))
                    throw new ArgumentOutOfRangeException("timeUnit", "Time unit must be less than 2^32 milliseconds");

                Occurrences = occurrences;
                TimeUnitMilliseconds = (int)timeUnit.TotalMilliseconds;

                // Create the semaphore, with the number of occurrences as the maximum count.
                _semaphore = new SemaphoreSlim(Occurrences, Occurrences);

                // Create a queue to hold the semaphore exit times.
                _exitTimes = new ConcurrentQueue<int>();

                // Create a timer to exit the semaphore. Use the time unit as the original
                // interval length because that's the earliest we will need to exit the semaphore.
                _exitTimer = new Timer(ExitTimerCallback, null, TimeUnitMilliseconds, -1);
            }
开发者ID:CamronBute,项目名称:yammerbot,代码行数:32,代码来源:YammerServiceManager.cs


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