示例#1
0
        private void ProcessPolling()
        {
            while (true)
            {
                if (_connectionEstablished)
                {
                    Poll();
                }
                else
                {
                    EstablishConnection();
                }

                Thread.Sleep(_connectionEstablished ? 5000 : 1000);
                PollingFinished?.Invoke();
            }
        }
示例#2
0
 public static void InvokePollingFinished()
 {
     PollingFinished?.Invoke();
 }
示例#3
0
        /// <summary>
        /// 开始轮询
        /// </summary>
        public void Start()
        {
            if (_pollingCancel != null)
            {
                return;
            }
            _pollingCancel = new CancellationTokenSource();

            new Thread(obj =>
            {
                var cancel = obj as CancellationTokenSource;
                while (!cancel.IsCancellationRequested)
                {
                    var semaphoreSlim = new SemaphoreSlim(_maxThreadCount);
                    var countdown     = new CountdownEvent(_pollingObjects.Count);
                    Dictionary <T_Object, T_Result> pollingResult = new Dictionary <T_Object, T_Result>();

                    List <Tuple <T_Object, T_Result> > notifyResult = new List <Tuple <T_Object, T_Result> >();

                    foreach (var ipe in _pollingObjects)
                    {
                        semaphoreSlim.Wait();
                        if (!cancel.IsCancellationRequested)
                        {
                            var task = new Task(obj1 =>
                            {
                                var cancel1 = obj1 as CancellationTokenSource;
                                if (cancel1.IsCancellationRequested)
                                {
                                    return;
                                }
                                var ep     = ipe;
                                var result = PollingDetailWork(ep);
                                PollingProgressing?.Invoke(ep, result);
                                lock (((ICollection)pollingResult).SyncRoot)
                                {
                                    pollingResult.Add(ep, result);
                                    if (_notifyCount > 0)
                                    {
                                        notifyResult.Add(new Tuple <T_Object, T_Result>(ep, result));
                                        if (notifyResult.Count >= _notifyCount)
                                        {
                                            PollingNotifyEvent.Invoke(notifyResult.ToDictionary(t => t.Item1, t => t.Item2));
                                            notifyResult.Clear();
                                        }
                                    }
                                }
                                countdown.Signal();
                                semaphoreSlim.Release();
                            }, cancel, cancel.Token);
                            task.Start();
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (!cancel.IsCancellationRequested)
                    {
                        countdown.Wait();
                        if (notifyResult.Count > 0)
                        {
                            PollingNotifyEvent.Invoke(notifyResult.ToDictionary(t => t.Item1, t => t.Item2));
                            notifyResult.Clear();
                        }
                        PollingFinished?.Invoke(pollingResult);
                    }
                    Thread.Sleep(_pollingInterval);
                }
            })
            {
                IsBackground = true
            }.Start(_pollingCancel);
        }