public void SetChannels(CasparChannelInformationList channels)
        {
            if (channels != null)
            {
                Dictionary<string, DeviceHolder> newDevices = new Dictionary<string, DeviceHolder>();
                Dictionary<string, CasparChannelInformation> newChannelInfos = new Dictionary<string, CasparChannelInformation>();
                foreach (CasparChannelInformation channelInfo in channels)
                {
                    if (!newDevices.ContainsKey(channelInfo.Hostname))
                    {
                        DeviceHolder device = null;

                        //Get device from old collection or create new
                        if (devices_ != null && devices_.ContainsKey(channelInfo.Hostname))
                        {
                            device = devices_[channelInfo.Hostname];
                            devices_.Remove(channelInfo.Hostname);

                            device.Channels.Clear();
                        }
                        else
                            device = CreateDevice(channelInfo.Hostname, channelInfo.Port);

                        //add device to new collection
                        if (device != null)
                        {
                            device.Channels.Add(channelInfo);
                            channelInfo.Online = device.HasValidConnection;
                            newDevices.Add(channelInfo.Hostname, device);
                        }

                    }
                    else
                    {
                        newDevices[channelInfo.Hostname].Channels.Add(channelInfo);
                        channelInfo.Online = newDevices[channelInfo.Hostname].HasValidConnection;
                    }

                    newChannelInfos.Add(channelInfo.Label, channelInfo);
                }

                if (connected)
                {
                    //disconnect from unused devices
                    if (devices_ != null)
                    {
                        foreach (DeviceHolder device in devices_.Values)
                        {
                            device.CasparDevice.Disconnect();
                        }
                    }

                    //connect to new devices
                    foreach (DeviceHolder device in newDevices.Values)
                    {
                        if (!device.CasparDevice.IsConnected)
                            device.CasparDevice.Connect();
                    }
                }

                //set new collection as current
                System.Threading.Interlocked.Exchange<Dictionary<string, DeviceHolder>>(ref devices_, newDevices);
                System.Threading.Interlocked.Exchange<Dictionary<string, CasparChannelInformation>>(ref channelInfos_, newChannelInfos);

                UpdateValidConnections();
            }
        }
        public void SetChannels(CasparChannelInformationList channels)
        {
            if (channels != null)
            {
                Dictionary <string, DeviceHolder>             newDevices      = new Dictionary <string, DeviceHolder>();
                Dictionary <string, CasparChannelInformation> newChannelInfos = new Dictionary <string, CasparChannelInformation>();
                foreach (CasparChannelInformation channelInfo in channels)
                {
                    if (!newDevices.ContainsKey(channelInfo.Hostname))
                    {
                        DeviceHolder device = null;

                        //Get device from old collection or create new
                        if (devices_ != null && devices_.ContainsKey(channelInfo.Hostname))
                        {
                            device = devices_[channelInfo.Hostname];
                            devices_.Remove(channelInfo.Hostname);

                            device.Channels.Clear();
                        }
                        else
                        {
                            device = CreateDevice(channelInfo.Hostname, channelInfo.Port);
                        }

                        //add device to new collection
                        if (device != null)
                        {
                            device.Channels.Add(channelInfo);
                            channelInfo.Online = device.HasValidConnection;
                            newDevices.Add(channelInfo.Hostname, device);
                        }
                    }
                    else
                    {
                        newDevices[channelInfo.Hostname].Channels.Add(channelInfo);
                        channelInfo.Online = newDevices[channelInfo.Hostname].HasValidConnection;
                    }

                    newChannelInfos.Add(channelInfo.Label, channelInfo);
                }

                if (connected)
                {
                    //disconnect from unused devices
                    if (devices_ != null)
                    {
                        foreach (DeviceHolder device in devices_.Values)
                        {
                            device.CasparDevice.Disconnect();
                        }
                    }

                    //connect to new devices
                    foreach (DeviceHolder device in newDevices.Values)
                    {
                        if (!device.CasparDevice.IsConnected)
                        {
                            device.CasparDevice.Connect();
                        }
                    }
                }

                //set new collection as current
                System.Threading.Interlocked.Exchange <Dictionary <string, DeviceHolder> >(ref devices_, newDevices);
                System.Threading.Interlocked.Exchange <Dictionary <string, CasparChannelInformation> >(ref channelInfos_, newChannelInfos);

                UpdateValidConnections();
            }
        }