private void ScaleChannels(ChannelScaleAction action, int count = 1) { //if (stopping) // return; for (int i = 0; i < count; i++) { if (cancelTokenSource.IsCancellationRequested) { break; } var newCount = 0; bool? destroyed = null; IPushChannel newChannel = default(IPushChannel); lock (channelsLock) { if (action == ChannelScaleAction.Create) { newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings); var chanWorker = new ChannelWorker(newChannel, DoChannelWork); chanWorker.WorkerTask.ContinueWith(t => { var ex = t.Exception; Log.Error("Channel Worker Failed Task: " + ex.ToString()); }, TaskContinuationOptions.OnlyOnFaulted); channels.Add(chanWorker); newCount = channels.Count; destroyed = false; } else if (action == ChannelScaleAction.Destroy && channels.Count > 0) { var channelOn = channels[0]; channels.RemoveAt(0); //Stop the channel worker, which will dispose the channel too channelOn.Dispose(); newCount = channels.Count; destroyed = true; } } if (destroyed.HasValue && !destroyed.Value) { var evt = OnChannelCreated; if (evt != null) { evt(this, newChannel); } } else if (destroyed.HasValue && destroyed.Value) { var evt = this.OnChannelDestroyed; if (evt != null) { evt(this); } } } }
private void ScaleChannels(ChannelScaleAction action, int count = 1) { //if (stopping) // return; for (int i = 0; i < count; i++) { if (cancelTokenSource.IsCancellationRequested) break; var newCount = 0; bool? destroyed = null; IPushChannel newChannel = default(IPushChannel); lock (channels) { if (action == ChannelScaleAction.Create) { newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings); var chanWorker = new ChannelWorker(newChannel, DoChannelWork); chanWorker.WorkerTask.ContinueWith(t => { var ex = t.Exception; Log.Error("Channel Worker Failed Task: " + ex.ToString()); }, TaskContinuationOptions.OnlyOnFaulted); channels.Add(chanWorker); newCount = channels.Count; destroyed = false; } else if (action == ChannelScaleAction.Destroy && channels.Count > 1) { var channelOn = channels[0]; channels.RemoveAt(0); //Stop the channel worker, which will dispose the channel too channelOn.Dispose (); newCount = channels.Count; destroyed = true; } } if (destroyed.HasValue && !destroyed.Value) { var evt = OnChannelCreated; if (evt != null) evt(this, newChannel); } else if (destroyed.HasValue && destroyed.Value) { var evt = this.OnChannelDestroyed; if (evt != null) evt(this); } } }
private void ScaleChannels(ChannelScaleAction action, int count = 1) { for (int i = 0; i < count; i++) { var newCount = 0; bool? destroyed = null; IPushChannel newChannel = default(IPushChannel); lock (channels) { if (action == ChannelScaleAction.Create) { newChannel = this.PushChannelFactory.CreateChannel(this.ChannelSettings); var chanWorker = new ChannelWorker(newChannel, DoChannelWork); chanWorker.WorkerTask.ContinueWith(t => { var ex = t.Exception; Log.Error("Channel Worker Failed Task: " + ex.ToString()); }, TaskContinuationOptions.OnlyOnFaulted); channels.Add(chanWorker); newCount = channels.Count; destroyed = false; } else if (action == ChannelScaleAction.Destroy && channels.Count > 1) { var channelOn = channels[0]; channels.RemoveAt(0); //Now stop the channel but let it finish channelOn.Channel.Dispose(); newCount = channels.Count; destroyed = true; } } if (destroyed.HasValue && !destroyed.Value) { var evt = OnChannelCreated; if (evt != null) evt(this, newChannel); } else if (destroyed.HasValue && destroyed.Value) { var evt = this.OnChannelDestroyed; if (evt != null) evt(this); } } }