Exemplo n.º 1
0
        private void Flush()
        {
            var toExecute = _queue.DequeueAll();

            if (toExecute == null || !toExecute.Any())
            {
                _flushPending = false;
                return;
            }

            var copy = toExecute.ToArray();

            _thread.Queue(() => { _executor.Execute(copy); });

            lock (_lock)
            {
                if (_queue.Count() > 0)
                {
                    // don't monopolize thread.
                    _thread.Queue(Flush);
                }
                else
                {
                    _flushPending = false;
                }
            }
        }
Exemplo n.º 2
0
 public int Count()
 {
     lock (_queue)
     {
         return(_queue.Count());
     }
 }
Exemplo n.º 3
0
        public void TestCount()
        {
            int expected = 15;

            for (int i = 0; i < expected; i++)
            {
                queue.Enqueue(i);
            }
            Assert.AreEqual(expected, queue.Count());
        }
Exemplo n.º 4
0
        public SpiderConfiguration Start()
        {
            _container = _builder.Build();
            if (!_container.IsRegisteredWithName <ICache>("Cache"))
            {
                throw new Exception("没有注册Cache");
            }
            if (!_container.IsRegisteredWithName <IQueue>("Queue"))
            {
                throw new Exception("没有注册Queue");
            }

            _queue = _container.ResolveNamed <IQueue>("Queue");
            _cache = _container.ResolveNamed <ICache>("Cache");

            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000 * 60);
                    Logger.Info($"QueueCount:{_queue.Count()}");
                }
            });

            for (var i = 0; i < _option.MaxSpiderThreadCount; i++)
            {
                var port = _option.LocalPort + i;
                Logger.ConsoleWrite($"线程:{i + 1} 端口:{port} 已启动监听...");
                Task.Run(() =>
                {
                    var spider          = new DHTSpider(new IPEndPoint(IPAddress.Any, port), _queue);
                    spider.NewMetadata += DHTSpider_NewMetadata;
                    spider.Start();
                });
                Thread.Sleep(1000);
            }

            for (var i = 0; i < _option.MaxDownLoadThreadCount; i++)
            {
                var id = i + 1;
                Logger.ConsoleWrite($"线程[{id}]开始下载");
                Task.Run(() =>
                {
                    Download(id);
                });
                Thread.Sleep(100);
            }


            return(_instance);
        }
Exemplo n.º 5
0
        public void refresh()
        {
            listBox.Items.Clear();

            if (Q != null)
            {
                if (Q.Count != 0)
                {
                    DelTool.Enabled = true;
                }
                textCount.Text = Convert.ToString(Q.Count());//!!!
                foreach (int i in Q)
                {
                    listBox.Items.Add(i);
                }
            }
            else if (Qs != null)
            {
                textCount.Text = Convert.ToString(Qs.Count());//!!!
                if (Qs.Count != 0)
                {
                    DelTool.Enabled = true;
                }
                foreach (string i in Qs)
                {
                    listBox.Items.Add(i);
                }
            }
            else
            {
                textCount.Text = "";
                textInfo.Text  = "Очереди не существует!";
            }
            bool IsEnabled = ((Qs != null) && (Qs.Count != 0)) || ((Q != null) && (Q.Count != 0));
            bool IsCreate  = (Qs != null) || (Q != null);

            ClearTool.Enabled    = IsEnabled;
            DelQueueTool.Enabled = IsCreate;
            ElemTool.Enabled     = IsCreate;
            UtilityTool.Enabled  = IsEnabled;
        }
Exemplo n.º 6
0
        public void Start()
        {
            _isExecuting = true;

            while (true)
            {
                GCUtils.FreeMemoryOptimizer(Constants.MinimalAvailableMemoryInPercentages);

                if (!_isExecuting || (_queue.Count() == 0 && _threadPool.Count == 0)) // Stop execution or all tasks done.
                {
                    break;
                }

                if (_threadPool.Count == _maxThreadsCount) // The whole pool is busy with threads.
                {
                    continue;
                }

                var task = _queue.Dequeue();
                if (task == null) // All tasks from the queue are pulled out.
                {
                    continue;
                }

                task.TaskDone += (sender, args) => { lock (_lock) _threadPool.Remove(args.Thread); };
                _totalTasks++;

                var thread = new Thread(() => task.Start())
                {
                    Name = "GZipTask" + _totalTasks, Priority = ThreadPriority.AboveNormal
                };
                lock (_lock) _threadPool.Add(thread);
                thread.Start();
            }

            _isExecuting = false;
            _totalTasks  = 0;
        }
 private void Assertion()
 {
     RunAssertion(() => Assert.AreEqual(1, inboxWorkQueue.Count()));
 }
Exemplo n.º 8
0
 public int Count()
 {
     return(_priQueue.Count());
 }