public ChannelWorker(IPushChannel channel, Action <IPushChannel, CancellationTokenSource> worker)
 {
     PushServiceBase.ChannelWorker channelWorker = this;
     this.Id = Guid.NewGuid().ToString();
     this.CancelTokenSource = new CancellationTokenSource();
     this.Channel           = channel;
     this.WorkerTask        = Task.Factory.StartNew((Action)(() => worker(channel, channelWorker.CancelTokenSource)), TaskCreationOptions.LongRunning);
 }
        private void ScaleChannels(ChannelScaleAction action, int count = 1)
        {
            for (int index = 0; index < count && !this.cancelTokenSource.IsCancellationRequested; ++index)
            {
                int          num         = 0;
                bool?        nullable    = new bool?();
                IPushChannel pushChannel = (IPushChannel)null;
                lock (this.channelsLock)
                {
                    switch (action)
                    {
                    case ChannelScaleAction.Create:
                        pushChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings);
                        PushServiceBase.ChannelWorker channelWorker = new PushServiceBase.ChannelWorker(pushChannel, new Action <IPushChannel, CancellationTokenSource>(this.DoChannelWork));
                        channelWorker.WorkerTask.ContinueWith((Action <Task>)(t => Log.Error("Channel Worker Failed Task: " + t.Exception.ToString())), TaskContinuationOptions.OnlyOnFaulted);
                        this.channels.Add(channelWorker);
                        num      = this.channels.Count;
                        nullable = new bool?(false);
                        break;

                    case ChannelScaleAction.Destroy:
                        if (this.channels.Count > 0)
                        {
                            PushServiceBase.ChannelWorker channel = this.channels[0];
                            this.channels.RemoveAt(0);
                            channel.Dispose();
                            num      = this.channels.Count;
                            nullable = new bool?(true);
                            break;
                        }
                        break;
                    }
                }
                if (nullable.HasValue && !nullable.Value)
                {
                    ChannelCreatedDelegate onChannelCreated = this.OnChannelCreated;
                    if (onChannelCreated != null)
                    {
                        onChannelCreated((object)this, pushChannel);
                    }
                }
                else if (nullable.HasValue && nullable.Value)
                {
                    ChannelDestroyedDelegate channelDestroyed = this.OnChannelDestroyed;
                    if (channelDestroyed != null)
                    {
                        channelDestroyed((object)this);
                    }
                }
            }
        }