From b187002e0d1f5345a77dea9e40644b187ec0d07c Mon Sep 17 00:00:00 2001 From: kevin-doolaeghe Date: Thu, 26 Jan 2023 18:05:42 +0100 Subject: [PATCH] Updated project to ClickOnce and .NET 7.0 --- NoSleepApp/Form1.Designer.cs | 61 +++++++++++++++--------------- NoSleepApp/Form1.cs | 72 +++++++++++++++--------------------- NoSleepApp/NoSleepApp.csproj | 2 +- NoSleepApp/Program.cs | 2 +- 4 files changed, 62 insertions(+), 75 deletions(-) diff --git a/NoSleepApp/Form1.Designer.cs b/NoSleepApp/Form1.Designer.cs index 1d0333f..cfbe789 100644 --- a/NoSleepApp/Form1.Designer.cs +++ b/NoSleepApp/Form1.Designer.cs @@ -1,6 +1,8 @@  namespace NoSleepApp { - partial class Form1 { + + partial class SleepForm { + /// /// Required designer variable. /// @@ -10,10 +12,8 @@ partial class Form1 { /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { + protected override void Dispose(bool disposing) { + if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); @@ -27,46 +27,45 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - this.button1 = new System.Windows.Forms.Button(); - this.label1 = new System.Windows.Forms.Label(); + this.sleepButton = new System.Windows.Forms.Button(); + this.disableMessageLabel = new System.Windows.Forms.Label(); this.SuspendLayout(); // - // button1 + // sleepButton // - this.button1.Cursor = System.Windows.Forms.Cursors.Hand; - this.button1.Location = new System.Drawing.Point(77, 12); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(75, 23); - this.button1.TabIndex = 0; - this.button1.Text = "Stop sleep"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.sleepButton.Cursor = System.Windows.Forms.Cursors.Hand; + this.sleepButton.Location = new System.Drawing.Point(77, 12); + this.sleepButton.Name = "sleepButton"; + this.sleepButton.Size = new System.Drawing.Size(75, 23); + this.sleepButton.TabIndex = 0; + this.sleepButton.Text = "Stop sleep"; + this.sleepButton.UseVisualStyleBackColor = true; + this.sleepButton.Click += new System.EventHandler(this.OnSleepButtonClick); // - // label1 + // disableMessageLabel // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(29, 38); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(168, 15); - this.label1.TabIndex = 1; - this.label1.Text = "Press Ctrl+C to disable service."; - this.label1.Click += new System.EventHandler(this.label1_Click); + this.disableMessageLabel.AutoSize = true; + this.disableMessageLabel.Location = new System.Drawing.Point(29, 38); + this.disableMessageLabel.Name = "disableMessageLabel"; + this.disableMessageLabel.Size = new System.Drawing.Size(168, 15); + this.disableMessageLabel.TabIndex = 1; + this.disableMessageLabel.Text = "Press Ctrl+C to disable service."; // - // Form1 + // SleepForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(224, 65); - this.Controls.Add(this.label1); - this.Controls.Add(this.button1); + this.Controls.Add(this.disableMessageLabel); + this.Controls.Add(this.sleepButton); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.KeyPreview = true; this.MaximizeBox = false; - this.Name = "Form1"; + this.Name = "SleepForm"; this.ShowInTaskbar = false; this.Text = "NoSleepApp"; this.TopMost = true; - this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); + this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownEvent); this.ResumeLayout(false); this.PerformLayout(); @@ -74,8 +73,8 @@ private void InitializeComponent() #endregion - private System.Windows.Forms.Button button1; - private System.Windows.Forms.Label label1; + private System.Windows.Forms.Button sleepButton; + private System.Windows.Forms.Label disableMessageLabel; } } diff --git a/NoSleepApp/Form1.cs b/NoSleepApp/Form1.cs index e911d59..6f7e78c 100644 --- a/NoSleepApp/Form1.cs +++ b/NoSleepApp/Form1.cs @@ -8,79 +8,67 @@ using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; +using System.Diagnostics; namespace NoSleepApp { - public partial class Form1 : Form { + public partial class SleepForm : System.Windows.Forms.Form { + private bool waiting; - private BackgroundWorker worker; + private readonly BackgroundWorker worker; - public Form1() - { + public SleepForm() { waiting = false; InitializeComponent(); - worker = new BackgroundWorker(); - worker.WorkerReportsProgress = true; + worker = new() { WorkerReportsProgress = true }; worker.DoWork += Wait; - worker.ProgressChanged += UpdateCursor; + worker.ProgressChanged += UpdateUi; worker.RunWorkerCompleted += EnableButton; } - private void Wait(object sender, DoWorkEventArgs e) - { + private void Wait(object sender, DoWorkEventArgs e) { BackgroundWorker bg = (BackgroundWorker)sender; int i = 0; - while (waiting) - { + while (waiting) { bg.ReportProgress(i); - // i += 10; - // Thread.Sleep(10); - int j; int sleepTime = 300; // 5 minutes - for (j = 0; j < sleepTime && waiting; j++) - Thread.Sleep(1000); + for (j = 0; j < sleepTime && waiting; j++) { + try { + Thread.Sleep(1000); + } catch (Exception ex) { + Debug.WriteLine(ex.Message); + } + } } } - private void UpdateCursor(object sender, ProgressChangedEventArgs e) - { - /* - int coef = 2; - double rad = Math.PI / 180; - int i = e.ProgressPercentage; - Cursor = new Cursor(Cursor.Current.Handle); - Cursor.Position = new Point(Cursor.Position.X + (int)(coef * Math.Cos(i * rad)), Cursor.Position.Y + (int)(coef * Math.Sin(i * rad))); - */ - SendKeys.Send("^{ESC}"); - Thread.Sleep(100); - SendKeys.Send("{ESC}"); + private void UpdateUi(object sender, ProgressChangedEventArgs e) { + try { + SendKeys.Send("^{ESC}"); + Thread.Sleep(100); + SendKeys.Send("{ESC}"); + } catch (Exception ex) { + Debug.WriteLine(ex.Message); + } } - private void EnableButton(object sender, RunWorkerCompletedEventArgs e) - { - button1.Enabled = true; + private void EnableButton(object sender, RunWorkerCompletedEventArgs e) { + sleepButton.Enabled = true; } - private void button1_Click(object sender, EventArgs e) - { + private void OnSleepButtonClick(object sender, EventArgs e) { waiting = true; worker.RunWorkerAsync(); - button1.Enabled = false; + sleepButton.Enabled = false; } - private void Form1_KeyDown(object sender, KeyEventArgs e) - { - if (e.KeyCode == Keys.C && e.Control && waiting) - { + private void OnKeyDownEvent(object sender, KeyEventArgs e) { + if (e.KeyCode == Keys.C && e.Control && waiting) { waiting = false; MessageBox.Show("Application is now disabled.", "Ctrl+C detected !"); } } - - private void label1_Click(object sender, EventArgs e) { - - } } } diff --git a/NoSleepApp/NoSleepApp.csproj b/NoSleepApp/NoSleepApp.csproj index 007ba49..fac0414 100644 --- a/NoSleepApp/NoSleepApp.csproj +++ b/NoSleepApp/NoSleepApp.csproj @@ -2,7 +2,7 @@ WinExe - net5.0-windows + net7.0-windows10.0.17763.0 true NoSleepApp.Program sleep.ico diff --git a/NoSleepApp/Program.cs b/NoSleepApp/Program.cs index b59b75b..cd787fe 100644 --- a/NoSleepApp/Program.cs +++ b/NoSleepApp/Program.cs @@ -15,7 +15,7 @@ static void Main() Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); + Application.Run(new SleepForm()); } } }