private async void DoDeletePrograms()
        {

            List<IProgram> SelectedPrograms = ReturnSelectedPrograms();
            if (SelectedPrograms.FirstOrDefault() != null)
            {
                if (SelectedPrograms.Count > 0)
                {
                    string question = (SelectedPrograms.Count == 1) ? "Delete program " + SelectedPrograms[0].Name + " ?" : "Delete these " + SelectedPrograms.Count + " programs ?";

                    DeleteProgramChannel form = new DeleteProgramChannel(question, "Delete Program(s)");

                    if (form.ShowDialog() == DialogResult.OK)
                    {

                        var assets = SelectedPrograms.Select(p => p.Asset).ToArray();

                        // Stop the programs which run
                        var programsrunning = SelectedPrograms.Where(p => p.State == ProgramState.Running);
                        if (programsrunning.Count() > 0)
                        {
                            var taskpstop = programsrunning.Select(p => StopProgramASync(p)).ToArray();
                            await Task.WhenAll(taskpstop);
                        }

                        // delete programs
                        SelectedPrograms.ToList().ForEach(p => TextBoxLogWriteLine("Deleting program '{0}'...", p.Name));
                        var tasks = SelectedPrograms.Select(p => ProgramExecuteAsync(p.DeleteAsync, p, "deleted")).ToArray();
                        bool Error = false;
                        try
                        {
                            await Task.WhenAll(tasks);
                        }
                        catch (Exception ex)
                        {
                            // Add useful information to the exception
                            TextBoxLogWriteLine("There is a problem when deleting a progam", true);
                            TextBoxLogWriteLine(ex);
                            Error = true;
                        }
                        DoRefreshGridProgramV(false);


                        if (form.DeleteAsset && Error == false)
                        {
                            assets.ToList().ForEach(a => TextBoxLogWriteLine("Deleting asset '{0}'", a.Name));
                            var tasksassets = assets.Select(a => a.DeleteAsync()).ToArray();
                            try
                            {
                                await Task.WhenAll(tasksassets);
                                TextBoxLogWriteLine("Asset(s) deletion done.");
                            }
                            catch (Exception ex)
                            {
                                // Add useful information to the exception
                                TextBoxLogWriteLine("There is a problem when deleting an asset", true);
                                TextBoxLogWriteLine(ex);
                            }
                            DoRefreshGridAssetV(false);
                        }
                    }
                }
            }
        }
        private async void DoDeletePrograms()
        {

            List<IProgram> SelectedPrograms = ReturnSelectedPrograms();
            if (SelectedPrograms.FirstOrDefault() != null)
            {
                if (SelectedPrograms.Count > 0)
                {
                    string question = (SelectedPrograms.Count == 1) ? "Delete program " + SelectedPrograms[0].Name + " ?" : "Delete these " + SelectedPrograms.Count + " programs ?";

                    DeleteProgramChannel form = new DeleteProgramChannel(question, "Delete Program(s)");

                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        foreach (IProgram myP in SelectedPrograms)
                        {
                            IAsset asset = myP.Asset;
                            await Task.Run(() => DeleteProgram(myP));
                            if (form.DeleteAsset)
                            {
                                if (myP.Asset != null)
                                {
                                    //delete
                                    TextBoxLogWriteLine("Deleting asset '{0}'", asset.Name);
                                    try
                                    {
                                        DeleteAsset(asset);
                                        if (AssetInfo.GetAsset(asset.Id, _context) == null)
                                        {
                                            TextBoxLogWriteLine("Deletion done.");
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        // Add useful information to the exception
                                        TextBoxLogWriteLine("There is a problem when deleting the asset {0}.", asset.Name, true);
                                        TextBoxLogWriteLine(ex);
                                    }
                                }
                            }
                        }
                        if (form.DeleteAsset)
                        {
                            DoRefreshGridAssetV(false);
                        }
                    }
                }
            }
        }
        private async void DoDeleteChannels()
        {
            List<IChannel> SelectedChannels = ReturnSelectedChannels();
            string hannelstr = SelectedChannels.Count > 1 ? "hannels" : "hannel";
            if (SelectedChannels.Count > 0)
            {
                List<string> ChannelSourceIDs = SelectedChannels.Select(c => c.Id).ToList();
                List<IProgram> Programs = _context.Programs.AsEnumerable().Where(p => ChannelSourceIDs.Contains(p.ChannelId)).ToList();

                if (Programs.Count == 0) // No program associated to the channel(s) to be deleted
                {
                    string question = (SelectedChannels.Count == 1) ? "Delete channel " + SelectedChannels[0].Name + " ?" : "Delete these " + SelectedChannels.Count + " channels ?";

                    if (System.Windows.Forms.MessageBox.Show(question, "C" + hannelstr + " deletion", System.Windows.Forms.MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        Task.Run(async () =>
                            {
                                // Stop the channels which run
                                var channelsrunning = SelectedChannels.Where(p => p.State == ChannelState.Running);
                                if (channelsrunning.Count() > 0)
                                {
                                    try
                                    {
                                        var taskcstop = channelsrunning.Select(c => StopChannelAsync(c)).ToArray();
                                        await Task.WhenAll(taskcstop);
                                    }
                                    catch (Exception ex)
                                    {
                                        // Add useful information to the exception
                                        TextBoxLogWriteLine("There is a problem when stopping a channel", true);
                                        TextBoxLogWriteLine(ex);
                                    }
                                }

                                // delete the channels
                                var taskcdel = SelectedChannels.Select(c => DeleteChannelAsync(c)).ToArray();
                                try
                                {
                                    await Task.WhenAll(taskcdel);
                                }

                                catch (Exception ex)
                                {
                                    // Add useful information to the exception
                                    TextBoxLogWriteLine("There is a problem when deleting a channel", true);
                                    TextBoxLogWriteLine(ex);
                                }
                                DoRefreshGridChannelV(false);
                            }
                           );
                    }
                }
                else // There are programs associated to the channel(s) to be deleted. We need to delete the programs
                {
                    string question = (Programs.Count == 1) ? string.Format("There is one program associated to the c{0}.\nDelete the c{0} and program '{1}' ?", hannelstr, Programs[0].Name)
                                                            : string.Format("There are {0} programs associated to the c{1}.\nDelete the c{1} and these programs ?", Programs.Count, hannelstr);

                    DeleteProgramChannel form = new DeleteProgramChannel(question, "Delete C" + hannelstr);
                    if (form.ShowDialog() == DialogResult.OK)
                    {


                        Task.Run(async () =>
                        {
                            var assets = Programs.Select(p => p.Asset).ToArray();

                            // Stop the programs which run
                            var programsrunning = Programs.Where(p => p.State == ProgramState.Running);
                            if (programsrunning.Count() > 0)
                            {
                                var taskpstop = programsrunning.Select(p => StopProgramASync(p)).ToArray();
                                await Task.WhenAll(taskpstop);
                            }

                            // delete programs
                            Programs.ToList().ForEach(p => TextBoxLogWriteLine("Program '{0}': deleting...", p.Name));
                            var tasks = Programs.Select(p => ProgramExecuteAsync(p.DeleteAsync, p, "deleted")).ToArray();
                            bool Error = false;
                            try
                            {
                                await Task.WhenAll(tasks);
                            }
                            catch (Exception ex)
                            {
                                // Add useful information to the exception
                                TextBoxLogWriteLine("There is a problem when deleting a progam", true);
                                TextBoxLogWriteLine(ex);
                                Error = true;
                            }
                            DoRefreshGridProgramV(false);


                            if (form.DeleteAsset && Error == false)
                            {
                                assets.ToList().ForEach(a => TextBoxLogWriteLine("Asset '{0}': deleting...", a.Name));
                                var tasksassets = assets.Select(a => a.DeleteAsync()).ToArray();
                                try
                                {
                                    await Task.WhenAll(tasksassets);
                                    TextBoxLogWriteLine("Asset(s) deletion done.");
                                }
                                catch (Exception ex)
                                {
                                    // Add useful information to the exception
                                    TextBoxLogWriteLine("There is a problem when deleting an asset", true);
                                    TextBoxLogWriteLine(ex);
                                }
                                DoRefreshGridAssetV(false);

                            }

                            // Stop the channels which run
                            var channelsrunning = SelectedChannels.Where(p => p.State == ChannelState.Running);
                            if (channelsrunning.Count() > 0)
                            {
                                try
                                {
                                    channelsrunning.ToList().ForEach(c => TextBoxLogWriteLine("Stopping channel '{0}'...", c.Name));
                                    var taskcstop = channelsrunning.Select(c => c.StopAsync()).ToArray();
                                    await Task.WhenAll(taskcstop);
                                }
                                catch (Exception ex)
                                {
                                    // Add useful information to the exception
                                    TextBoxLogWriteLine("There is a problem when stopping a channel", true);
                                    TextBoxLogWriteLine(ex);
                                }
                            }

                            // delete the channels
                            var taskcdel = SelectedChannels.Select(c => DeleteChannelAsync(c)).ToArray();
                            try
                            {

                                await Task.WhenAll(taskcdel);
                            }

                            catch (Exception ex)
                            {
                                // Add useful information to the exception
                                TextBoxLogWriteLine("There is a problem when deleting a channel", true);
                                TextBoxLogWriteLine(ex);
                            }
                            DoRefreshGridChannelV(false);

                        }
                         );

                    }
                }
            }
        }
        private async void DoDeleteChannels()
        {
            List<IChannel> SelectedChannels = ReturnSelectedChannels();
            string hannelstr = SelectedChannels.Count > 1 ? "hannels" : "hannel";
            if (SelectedChannels.Count > 0)
            {
                List<string> ChannelSourceIDs = SelectedChannels.Select(c => c.Id).ToList();
                List<IProgram> Programs = _context.Programs.AsEnumerable().Where(p => ChannelSourceIDs.Contains(p.ChannelId)).ToList();

                if (Programs.Count == 0) // No program associated to the channel(s) to be deleted
                {
                    string question = (SelectedChannels.Count == 1) ? "Delete channel " + SelectedChannels[0].Name + " ?" : "Delete these " + SelectedChannels.Count + " channels ?";

                    if (System.Windows.Forms.MessageBox.Show(question, "C" + hannelstr + " deletion", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        foreach (IChannel myC in ReturnSelectedChannels())
                        {
                            DeleteChannel(myC);
                        }
                    }
                }
                else // There are programs associated to the channel(s) to be deleted. We need to delete the programs
                {
                    string question = (Programs.Count == 1) ? string.Format("There is one program associated to the c{0}.\nDelete the c{0} and program '{1}' ?", hannelstr, Programs[0].Name)
                                                            : string.Format("There are {0} programs associated to the c{1}.\nDelete the c{1} and these programs ?", Programs.Count, hannelstr);

                    DeleteProgramChannel form = new DeleteProgramChannel(question, "Delete C" + hannelstr);
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        foreach (IProgram myP in Programs)
                        {
                            IAsset asset = myP.Asset;
                            await Task.Run(() => DeleteProgram(myP));
                            //DeleteProgram(myP);
                            if (form.DeleteAsset)
                            {
                                if (myP.Asset != null)
                                {
                                    //delete
                                    TextBoxLogWriteLine("Deleting asset '{0}'", asset.Name);
                                    try
                                    {
                                        DeleteAsset(asset);
                                        if (AssetInfo.GetAsset(asset.Id, _context) == null) TextBoxLogWriteLine("Deletion done.");
                                    }
                                    catch (Exception ex)
                                    {
                                        // Add useful information to the exception
                                        TextBoxLogWriteLine("There is a problem when deleting the asset {0}.", asset.Name, true);
                                        TextBoxLogWriteLine(ex);
                                    }
                                }
                            }
                        }
                        if (form.DeleteAsset)
                        {
                            DoRefreshGridAssetV(false);
                        }

                        foreach (IChannel myC in ReturnSelectedChannels())
                        {
                            DeleteChannel(myC);
                        }
                    }
                }
            }
        }