Skip to content

Commit

Permalink
More refactors and optimisations
Browse files Browse the repository at this point in the history
- Return index of fan that was read when GetFanSpeed, GetFanRPM, and GetTemp commands are issued

- ProgressDialog now supports generic return type (instead of just Object)

- Remove Cancel and progress reporting functionality from ProgressDialog since they aren't used

- Several small optimisations across every project

WARNING: this version of YAMDCC is not compatible with previous commits!
  • Loading branch information
Sparronator9999 committed Jan 16, 2025
1 parent 8fcebda commit 5f07394
Show file tree
Hide file tree
Showing 23 changed files with 465 additions and 588 deletions.
46 changes: 7 additions & 39 deletions YAMDCC.Common/Dialogs/ProgressDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

136 changes: 16 additions & 120 deletions YAMDCC.Common/Dialogs/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,77 +15,45 @@
// YAMDCC. If not, see <https://www.gnu.org/licenses/>.

using System;
using System.ComponentModel;
using System.Globalization;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YAMDCC.Common.Dialogs;

public sealed partial class ProgressDialog : Form
public sealed partial class ProgressDialog<TResult> : Form
{
#region Disable close button
private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle |= CP_NOCLOSE_BUTTON;
return myCp;
CreateParams cParams = base.CreateParams;
cParams.ClassStyle |= 0x200; // CP_NOCLOSE_BUTTON
return cParams;
}
}
#endregion

public bool Cancelled { get; set; }
public object Result { get; set; }
public TResult Result { get; set; }

private readonly object Argument;
private readonly string Caption;
private readonly Action<DoWorkEventArgs> DoWork;
private readonly Func<TResult> DoWork;

private readonly BackgroundWorker Worker = new();
private readonly Timer DisplayTimer = new();

/// <inheritdoc cref="ProgressDialog(string, Action{DoWorkEventArgs}, object, bool, bool)"/>
public ProgressDialog(
string caption,
Action<DoWorkEventArgs> doWork,
bool reportsProgress = false,
bool canCancel = false)
: this(caption, doWork, null, reportsProgress, canCancel)
{ }

/// <summary>
/// Initialises a new instance of the <see cref="ProgressDialog"/> class.
/// </summary>
/// <param name="caption">
/// The window caption to use.
/// </param>
/// <param name="doWork">
/// The <see cref="Action"/> to run when showing this window.
/// </param>
/// <param name="argument">
/// The argument to pass to <paramref name="doWork"/>.
/// </param>
/// <param name="reportsProgress">
/// Set to <see langword="true"/> if <paramref name="doWork"/>
/// reports progress, otherwise <see langword="false"/>.
/// </param>
/// <param name="canCancel">
/// Set to <see langword="true"/> if <paramref name="doWork"/>
/// supports cancellation, otherwise <see langword="false"/>.
/// The <see cref="Func{T}"/> to run when showing this window.
/// </param>
/// <exception cref="ArgumentNullException"/>
public ProgressDialog(
string caption,
Action<DoWorkEventArgs> doWork,
object argument,
bool reportsProgress = false,
bool canCancel = false)
public ProgressDialog(string caption, Func<TResult> doWork)
{
Opacity = 0;
Argument = argument;
InitializeComponent();

// sanity check
Expand All @@ -96,96 +64,24 @@ public ProgressDialog(
DoWork = doWork;

// set title text
Caption = caption ?? "Please wait...";
if (reportsProgress && caption is null)
{
Caption += " ({0}% complete)";
}
SetTitle(Caption);
lblCaption.Text = string.Format(CultureInfo.InvariantCulture, caption ?? "Please wait...", pbProgress.Value);

// event setup
Worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
if (reportsProgress)
{
Worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);
}
else
{
pbProgress.Style = ProgressBarStyle.Marquee;
}

// cancel support stuff
if (canCancel)
{
Worker.WorkerSupportsCancellation = true;
}
else
{
btnCancel.Enabled = false;
btnCancel.Visible = false;
}
pbProgress.Style = ProgressBarStyle.Marquee;

DisplayTimer.Interval = 1000;
DisplayTimer.Tick += new EventHandler(DisplayTimer_Tick);
DisplayTimer.Tick += new EventHandler(ShowProgress);
}

private void ProgressDialog_Load(object sender, EventArgs e)
private async void OnLoad(object sender, EventArgs e)
{
DisplayTimer.Start();
Worker.RunWorkerAsync(Argument);
Result = await Task.Run(DoWork);
Close();
}

private void DisplayTimer_Tick(object sender, EventArgs e)
private void ShowProgress(object sender, EventArgs e)
{
Opacity = 1;
DisplayTimer.Stop();
}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
DoWork?.Invoke(e);
}

private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage < 0)
{
pbProgress.Style = ProgressBarStyle.Marquee;
}
else
{
pbProgress.Style = ProgressBarStyle.Blocks;
pbProgress.Value = e.ProgressPercentage;
}
SetTitle(Caption);
}

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error is not null)
{
throw e.Error;
}
Cancelled = e.Cancelled;
Result = e.Result;
Worker.Dispose();
Close();
}

private void btnCancel_Click(object sender, EventArgs e)
{
if (Worker.WorkerSupportsCancellation && Worker.IsBusy && !Worker.CancellationPending)
{
btnCancel.Enabled = false;
Worker.CancelAsync();
Text = "Cancelling...";
pbProgress.Style = ProgressBarStyle.Marquee;
}
}

private void SetTitle(string title)
{
lblCaption.Text = string.Format(CultureInfo.InvariantCulture, title, pbProgress.Value);
}
}
6 changes: 0 additions & 6 deletions YAMDCC.Common/Dialogs/ProgressDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,4 @@
<metadata name="tblMain.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="flwButtons.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
<metadata name="flwButtons.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>False</value>
</metadata>
</root>
5 changes: 3 additions & 2 deletions YAMDCC.Common/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ internal static class Strings
/// </returns>
public static string GetString(string name, params object[] args)
{
CultureInfo ci = CultureInfo.InvariantCulture;
resMan ??= new ResourceManager(typeof(Strings));
string temp = resMan.GetString(name, CultureInfo.InvariantCulture);

string temp = resMan.GetString(name, ci);
return temp is null
? null
: string.Format(CultureInfo.InvariantCulture, temp, args);
: string.Format(ci, temp, args);
}
}
7 changes: 3 additions & 4 deletions YAMDCC.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public static int RunCmd(string exe, string args, bool waitExit = true)
/// </summary>
/// <returns>
/// The computer model if the function succeeds,
/// otherwise <c>null</c>.
/// otherwise <see cref="string.Empty"/>.'
/// </returns>
public static string GetPCModel()
{
Expand All @@ -456,7 +456,7 @@ public static string GetPCModel()
/// </summary>
/// <returns>
/// The computer manufacturer if the function succeeds,
/// otherwise <c>null</c>.
/// otherwise <see cref="string.Empty"/>.
/// </returns>
public static string GetPCManufacturer()
{
Expand All @@ -467,8 +467,7 @@ private static string GetBIOSRegValue(string name)
{
using (RegistryKey biosKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DESCRIPTION\System\BIOS"))
{
return (string)biosKey?.GetValue(name, null);
return ((string)biosKey?.GetValue(name, string.Empty)).Trim();
}
}

}
Loading

0 comments on commit 5f07394

Please sign in to comment.