Beispiel #1
0
        public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
        {
            var source  = new TaskCompletionSource <object>();
            var delayed = TaskMonitorService.GetService().Delay((long)millisecondsDelay, source);

            if (cancellationToken != CancellationToken.None)
            {
                cancellationToken.Register(() =>
                {
                    delayed.Cancel(false);
                    source.SetCanceled();
                });
            }

            return(source.Task);
        }
Beispiel #2
0
        private void ScheduleCheckThreadCount()
        {
            VerboseLog("{0:000}|ScheduleCheckThreadCount: CoreSize: {1} maxSize: {2} queuelen: {3}; active {4}; pool size: {5}; futureDelay: {7}s; future: {6};",
                       Thread.CurrentThread.Id, _threadPool.CorePoolSize, _threadPool.MaximumPoolSize, _threadPool.Queue.Size(), _threadPool.ActiveCount, _threadPool.PoolSize,
                       _checkIncreaseThreadCount, _checkIncreaseThreadCount != null?_checkIncreaseThreadCount.GetDelay(TimeUnit.SECONDS).ToString(): "-");

            // since ActiveCount might not be accurate, perform the check with a margin.
            if (_threadPool.ActiveCount < _threadPool.CorePoolSize - 1 ||
                _threadPool.CorePoolSize >= _threadPool.MaximumPoolSize)
            {
                return;
            }

            if (_checkIncreaseThreadCount == null)
            {
                var  future   = TaskMonitorService.GetService().Delay(30000L, CheckThreadCount);
                bool setValue = Interlocked.CompareExchange(ref _checkIncreaseThreadCount, future, null) == null;
                if (!setValue)
                {
                    future.Cancel(false);
                }
            }
        }