diff --git a/HeliosPlus.Shared/ProfileRepository.cs b/HeliosPlus.Shared/ProfileRepository.cs index 4602232..59859ac 100644 --- a/HeliosPlus.Shared/ProfileRepository.cs +++ b/HeliosPlus.Shared/ProfileRepository.cs @@ -27,6 +27,7 @@ using System.Net.NetworkInformation; using NvAPIWrapper.Mosaic; using NvAPIWrapper.Native.Mosaic; + namespace HeliosPlus.Shared { diff --git a/HeliosPlus/Program.cs b/HeliosPlus/Program.cs index 2f16e0d..15932da 100644 --- a/HeliosPlus/Program.cs +++ b/HeliosPlus/Program.cs @@ -353,22 +353,53 @@ namespace HeliosPlus { try { - // Set up the UI forms to show - ApplyingProfileForm timeoutForm = new ApplyingProfileForm(3, 0, $"Applying Profile '{profile.Name}'", $"Press ESC to timeout"); - ApplyingProfileForm topologyForm = new ApplyingProfileForm(0, 30, $"Applying Profile '{profile.Name}' Topology"); - ApplyingProfileForm pathInfoForm = new ApplyingProfileForm(0, 30, $"Applying Profile '{profile.Name}' Path"); - - topologyForm.ShowDialog(); - // Now lets start by changing the display topology - Task applyTopologyTask = Task.Run(() => + // Now lets prepare changing the display topology task + Task applyTopologyTask = new Task(() => { - Console.WriteLine("ProfileRepository/SaveShortcutIconToCache : Applying Profile Topology " + profile.Name); - ProfileRepository.ApplyTopology(profile); + Console.WriteLine("Program/ApplyProfile : Applying Profile Topology " + profile.Name); + if (!ProfileRepository.ApplyTopology(profile)) + { + // Somehow return that this profile topology didn't apply + } }); - applyTopologyTask.Wait(); - topologyForm.Close(); - if (applyTopologyTask.IsCompleted) + Task applyPathInfoTask = new Task(() => { + Console.WriteLine("Program/ApplyProfile : Applying Profile Path " + profile.Name); + if (!ProfileRepository.ApplyPathInfo(profile)) + { + // Somehow return that this profile path info didn't apply + } + + }); + + // Set up the UI forms to show + ApplyingProfileForm timeoutForm = new ApplyingProfileForm(null, 3, $"Applying Profile '{profile.Name}'", "Press ESC to cancel!", Color.Blue, true); + ApplyingProfileForm topologyForm = new ApplyingProfileForm(applyTopologyTask, 30, $"Applying Profile '{profile.Name}' Topology", "Step one of two...", Color.Red); + ApplyingProfileForm pathInfoForm = new ApplyingProfileForm(applyPathInfoTask, 30, $"Applying Profile '{profile.Name}' Path", "Step two of two...", Color.Green); + + if (timeoutForm.ShowDialog() == DialogResult.Cancel) + { + return false; + } + + topologyForm.ShowDialog(); + applyTopologyTask.Wait(); + + if (applyTopologyTask.IsFaulted) + Console.WriteLine("Program/ApplyProfile : Applying Profile Topology stage failed to complete"); + + pathInfoForm.ShowDialog(); + applyPathInfoTask.Wait(); + + if (applyPathInfoTask.IsFaulted) + Console.WriteLine("Program/ApplyProfile : Applying Profile PathInfo stage failed to complete"); + + if (applyTopologyTask.IsCompleted && applyPathInfoTask.IsCompleted) + return true; + else + return false; + +/* if (applyTopologyTask.IsCompleted) { pathInfoForm.ShowDialog(); Task applyPathInfoTask = Task.Run(() => { @@ -389,7 +420,7 @@ namespace HeliosPlus { { return false; } - + */ } catch (Exception ex) diff --git a/HeliosPlus/UIForms/ApplyingProfileForm.Designer.cs b/HeliosPlus/UIForms/ApplyingProfileForm.Designer.cs index 8decd29..a925ba1 100644 --- a/HeliosPlus/UIForms/ApplyingProfileForm.Designer.cs +++ b/HeliosPlus/UIForms/ApplyingProfileForm.Designer.cs @@ -34,7 +34,6 @@ this.progressBar = new CircularProgressBar.CircularProgressBar(); this.lbl_message = new System.Windows.Forms.Label(); this.t_countdown = new System.Windows.Forms.Timer(this.components); - this.t_cancellation = new System.Windows.Forms.Timer(this.components); this.progressPanel.SuspendLayout(); this.SuspendLayout(); // @@ -105,11 +104,6 @@ this.t_countdown.Interval = 1000; this.t_countdown.Tick += new System.EventHandler(this.t_countdown_Tick); // - // t_cancellation - // - this.t_cancellation.Interval = 1000; - this.t_cancellation.Tick += new System.EventHandler(this.t_cancellation_Tick); - // // ApplyingProfileForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -144,6 +138,5 @@ private System.Windows.Forms.Label lbl_sub_message; private System.Windows.Forms.Label lbl_message; private System.Windows.Forms.Timer t_countdown; - private System.Windows.Forms.Timer t_cancellation; } } \ No newline at end of file diff --git a/HeliosPlus/UIForms/ApplyingProfileForm.cs b/HeliosPlus/UIForms/ApplyingProfileForm.cs index b767a6b..8a27ec4 100644 --- a/HeliosPlus/UIForms/ApplyingProfileForm.cs +++ b/HeliosPlus/UIForms/ApplyingProfileForm.cs @@ -21,7 +21,6 @@ namespace HeliosPlus.UIForms private int _displayChangeDelta; private int _lastCount; private bool _isClosing; - private int _cancellationCounter; public ApplyingProfileForm() { @@ -34,21 +33,25 @@ namespace HeliosPlus.UIForms Reposition(); } - public ApplyingProfileForm(int cancellationTimeout = 0, int countdown = 0, string title = null, string message = null, int displayChangeMaxDelta = 5) : this() + public ApplyingProfileForm(Task taskToRun = null, int countdown = 0, string title = null, string message = null, Color progressColor = default(Color), bool cancellable = false, int displayChangeMaxDelta = 5) : this() { - _cancellationCounter = cancellationTimeout; _countdownCounter = countdown; _lastCount = _countdownCounter; _displayChangeMaxDelta = displayChangeMaxDelta; - if (!string.IsNullOrEmpty(title)) CountdownTitle = title; - if (!string.IsNullOrEmpty(message)) CountdownMessage = message; + Cancellable = cancellable; + TaskToRun = taskToRun; + if (progressColor.Equals(default(Color))) + progressColor = Color.Red; + ProgressColor = progressColor; + if (!string.IsNullOrEmpty(title)) Title = title; + if (!string.IsNullOrEmpty(message)) Message = message; } - public string CancellationTitle { get; set; } = "Starting in ..."; - public string CancellationMessage { get; set; } = "Please press ESC to cancel"; - - public string CountdownTitle { get; set; } = "Please wait..."; - public string CountdownMessage { get; set; } = "It won't be long now!"; + public string Title { get; set; } = "Please wait..."; + public string Message { get; set; } = "It won't be long now!"; + public Color ProgressColor { get; set; } = Color.OrangeRed; + public bool Cancellable{ get; set; } = false; + public Task TaskToRun { get; set; } = null; protected override void OnKeyDown(KeyEventArgs e) { @@ -77,9 +80,8 @@ namespace HeliosPlus.UIForms return base.ProcessCmdKey(ref msg, keyData); } - if (t_cancellation.Enabled) + if (Cancellable) { - t_cancellation.Stop(); t_countdown.Stop(); DialogResult = DialogResult.Cancel; Close(); @@ -106,11 +108,13 @@ namespace HeliosPlus.UIForms { if (_countdownCounter > 0) { - lbl_message.Text = CountdownTitle; - lbl_sub_message.Text = CountdownMessage; - progressBar.ProgressColor = Color.OrangeRed; + lbl_message.Text = Title; + lbl_sub_message.Text = Message; + progressBar.ProgressColor = ProgressColor; progressBar.Text = (progressBar.Value = progressBar.Maximum = _countdownCounter).ToString(); t_countdown.Start(); + if (TaskToRun is Task) + TaskToRun.Start(); } else { @@ -119,6 +123,8 @@ namespace HeliosPlus.UIForms progressBar.Maximum = 100; progressBar.Value = 50; progressBar.Style = ProgressBarStyle.Marquee; + if (TaskToRun is Task) + TaskToRun.Start(); DialogResult = DialogResult.OK; Close(); } @@ -126,23 +132,6 @@ namespace HeliosPlus.UIForms HandleDisplayChangeDelta(); } - private void DoCancellationTimeout() - { - if (_cancellationCounter > 0) - { - lbl_message.Text = CancellationTitle; - lbl_sub_message.Text = CancellationMessage; - progressBar.ProgressColor = Color.DodgerBlue; - progressBar.Text = (progressBar.Value = progressBar.Maximum = _cancellationCounter).ToString(); - t_cancellation.Start(); - } - else - { - DoCountdown(); - } - HandleDisplayChangeDelta(); - } - private void Reposition() { lock (_progressPositions) @@ -213,7 +202,7 @@ namespace HeliosPlus.UIForms Console.WriteLine($"ApplyingProfileForm/ApplyingProfileForm_Shown exception: {ex.Message}: {ex.StackTrace} - {ex.InnerException}"); // ignored } - }, this), new SafeInvoker(DoCancellationTimeout, this)); + }, this), new SafeInvoker(DoCountdown, this)); } private void t_countdown_Tick(object sender, EventArgs e) @@ -235,22 +224,6 @@ namespace HeliosPlus.UIForms Reposition(); } - private void t_cancellation_Tick(object sender, EventArgs e) - { - if (_cancellationCounter < 0) - { - t_cancellation.Stop(); - DoCountdown(); - - return; - } - - progressBar.Value = _cancellationCounter; - progressBar.Text = progressBar.Value.ToString(); - _cancellationCounter--; - Reposition(); - } - protected override void WndProc(ref Message m) { const int WM_SETTINGCHANGE = 0x001A; diff --git a/HeliosPlus/UIForms/ApplyingProfileForm.resx b/HeliosPlus/UIForms/ApplyingProfileForm.resx index 305cd01..e28cd72 100644 --- a/HeliosPlus/UIForms/ApplyingProfileForm.resx +++ b/HeliosPlus/UIForms/ApplyingProfileForm.resx @@ -120,7 +120,4 @@ 143, 17 - - 17, 17 - \ No newline at end of file