/// <summary> /// Apply the tasks specified in the current task queue /// </summary> public static void Apply(this ConfigurationContext me) { if (me.ConfigurationTasks.Count == 0) { return; } // Add the save task if (!me.ConfigurationTasks.OfType <SaveConfigurationTask>().Any()) { me.ConfigurationTasks.Add(new SaveConfigurationTask()); } // Is the WindowsService installed? var rstr = new RestartServiceTask(); if (rstr.VerifyState(me.Configuration)) { me.ConfigurationTasks.Add(new RestartServiceTask()); } var confirmDlg = new frmTaskList(); if (confirmDlg.ShowDialog() != DialogResult.OK) { return; } var progress = new frmProgress(); progress.Show(); try { // Do work in background thread here bool complete = false; Exception errCode = null; var exeThd = new Thread(() => { try { AuthenticationContext.Current = new AuthenticationContext(AuthenticationContext.SystemPrincipal); int i = 0, t = me.ConfigurationTasks.Count; var tasks = me.ConfigurationTasks.ToArray(); foreach (var ct in tasks) { ct.ProgressChanged += (o, e) => { progress.ActionStatusText = e.State?.ToString() ?? "..."; progress.ActionStatus = (int)(e.Progress * 100); progress.OverallStatus = (int)((((float)i / t) + (e.Progress * 1.0f / t)) * 100); }; progress.OverallStatusText = $"Applying {ct.Feature.Name}"; if (ct.VerifyState(me.Configuration)) { ct.Execute(me.Configuration); } me.ConfigurationTasks.Remove(ct); progress.OverallStatus = (int)(((float)++i / t) * 100.0); } } catch (Exception e) { Trace.TraceError($"Error on component: {e}"); errCode = e; } finally { complete = true; } }); exeThd.Start(); while (!complete) { Application.DoEvents(); } if (errCode != null) { throw errCode; } progress.OverallStatusText = "Reloading Configuration..."; progress.OverallStatus = 100; progress.ActionStatusText = "Reloading Configuration..."; progress.ActionStatus = 50; me.RestartContext(); progress.ActionStatusText = "Reloading Configuration..."; progress.ActionStatus = 100; } catch (Exception e) { // TODO: Rollback MessageBox.Show($"Error applying configuration: {e.Message}"); Trace.TraceError("Error applying configuration: {0}", e); } finally { progress.Close(); } }