Updated Licensing, Improved error messaging, added confirmation for actions scheduled less than 15s in advance, Added restart option.

This commit is contained in:
Kamil118 committed 2026-08-22 14:46:48 +02:00
1 parent ed410661d3
commit 8b65435924
6 files changed
+246 -12

No files matched your search

+18 -5
View File
@@ -36,6 +36,7 @@
textBox1 = new TextBox(); textBox1 = new TextBox();
tableLayoutPanel2 = new TableLayoutPanel(); tableLayoutPanel2 = new TableLayoutPanel();
flowLayoutPanel1 = new FlowLayoutPanel(); flowLayoutPanel1 = new FlowLayoutPanel();
button1 = new Button();
tableLayoutPanel2.SuspendLayout(); tableLayoutPanel2.SuspendLayout();
flowLayoutPanel1.SuspendLayout(); flowLayoutPanel1.SuspendLayout();
SuspendLayout(); SuspendLayout();
@@ -53,7 +54,7 @@
// //
// shutdownButton // shutdownButton
// //
shutdownButton.Location = new Point(185, 3); shutdownButton.Location = new Point(263, 3);
shutdownButton.Margin = new Padding(3, 3, 0, 3); shutdownButton.Margin = new Padding(3, 3, 0, 3);
shutdownButton.Name = "shutdownButton"; shutdownButton.Name = "shutdownButton";
shutdownButton.Size = new Size(75, 23); shutdownButton.Size = new Size(75, 23);
@@ -64,7 +65,7 @@
// //
// suspendButton // suspendButton
// //
suspendButton.Location = new Point(104, 3); suspendButton.Location = new Point(182, 3);
suspendButton.Name = "suspendButton"; suspendButton.Name = "suspendButton";
suspendButton.Size = new Size(75, 23); suspendButton.Size = new Size(75, 23);
suspendButton.TabIndex = 2; suspendButton.TabIndex = 2;
@@ -74,7 +75,7 @@
// //
// hibernateButton // hibernateButton
// //
hibernateButton.Location = new Point(23, 3); hibernateButton.Location = new Point(101, 3);
hibernateButton.Name = "hibernateButton"; hibernateButton.Name = "hibernateButton";
hibernateButton.Size = new Size(75, 23); hibernateButton.Size = new Size(75, 23);
hibernateButton.TabIndex = 3; hibernateButton.TabIndex = 3;
@@ -114,12 +115,23 @@
flowLayoutPanel1.Controls.Add(shutdownButton); flowLayoutPanel1.Controls.Add(shutdownButton);
flowLayoutPanel1.Controls.Add(suspendButton); flowLayoutPanel1.Controls.Add(suspendButton);
flowLayoutPanel1.Controls.Add(hibernateButton); flowLayoutPanel1.Controls.Add(hibernateButton);
flowLayoutPanel1.Controls.Add(button1);
flowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft; flowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft;
flowLayoutPanel1.Location = new Point(221, 33); flowLayoutPanel1.Location = new Point(143, 33);
flowLayoutPanel1.Name = "flowLayoutPanel1"; flowLayoutPanel1.Name = "flowLayoutPanel1";
flowLayoutPanel1.Size = new Size(260, 41); flowLayoutPanel1.Size = new Size(338, 41);
flowLayoutPanel1.TabIndex = 7; flowLayoutPanel1.TabIndex = 7;
// //
// button1
//
button1.Location = new Point(20, 3);
button1.Name = "button1";
button1.Size = new Size(75, 23);
button1.TabIndex = 4;
button1.Text = "Restart";
button1.UseVisualStyleBackColor = true;
button1.Click += restart_Click;
//
// GoToSleep // GoToSleep
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
@@ -145,5 +157,6 @@
private TextBox textBox1; private TextBox textBox1;
private TableLayoutPanel tableLayoutPanel2; private TableLayoutPanel tableLayoutPanel2;
private FlowLayoutPanel flowLayoutPanel1; private FlowLayoutPanel flowLayoutPanel1;
private Button button1;
} }
} }
+17 -4
View File
@@ -41,8 +41,9 @@ namespace GoToSleep
{ {
manager.shutdown(getTimeString()); manager.shutdown(getTimeString());
} }
catch (Exception ex) { catch (Exception ex)
MessageBox.Show(ex.Message); {
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
@@ -54,7 +55,7 @@ namespace GoToSleep
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
@@ -66,7 +67,19 @@ namespace GoToSleep
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void restart_Click(object sender, EventArgs e)
{
try
{
manager.restart(getTimeString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
+10
View File
@@ -38,6 +38,16 @@ namespace GoToSleep
Process.Start(psi); Process.Start(psi);
}); });
} }
public static PowerDownData Restart(DateTime when)
{
return new PowerDownData(PowerDownType.Shutdown, when, () => {
var psi = new ProcessStartInfo("shutdown", "/r /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
});
}
public static PowerDownData Suspend(DateTime when) public static PowerDownData Suspend(DateTime when)
{ {
return new PowerDownData(PowerDownType.Suspend, when, () => { return new PowerDownData(PowerDownType.Suspend, when, () => {
+58 -2
View File
@@ -13,11 +13,14 @@ namespace GoToSleep
{ {
internal class SystemPowerManager internal class SystemPowerManager
{ {
private static int TIMER_WARNING_SECONDS = 15;
[Serializable] [Serializable]
class DatetimeStringException : Exception class DatetimeStringException : Exception
{ {
public DatetimeStringException(string message) : base(message) { } public DatetimeStringException(string message) : base(message) { }
public DatetimeStringException(Exception e) : base("Conversion failed.", e) { } public DatetimeStringException(Exception e) : base("Conversion from string to time failed.", e) { }
} }
[Serializable] [Serializable]
@@ -62,7 +65,6 @@ namespace GoToSleep
try try
{ {
timeString = prepareTimeString(timeString); timeString = prepareTimeString(timeString);
//var tmp = parser.Parse(timeString);
List<ModelResult>? results = null; List<ModelResult>? results = null;
@@ -77,6 +79,7 @@ namespace GoToSleep
if (results == null || results.Count == 0) if (results == null || results.Count == 0)
{ {
//try converting using english as fallback
results = DateTimeRecognizer.RecognizeDateTime(timeString, Culture.English); results = DateTimeRecognizer.RecognizeDateTime(timeString, Culture.English);
if (results == null || results.Count == 0) if (results == null || results.Count == 0)
{ {
@@ -104,6 +107,21 @@ namespace GoToSleep
return result; return result;
} }
bool confirmDate(DateTime target, string action)
{
if (target <= DateTime.Now.AddSeconds(TIMER_WARNING_SECONDS))
{
//the number will be always off by a fraction of a second, so round it up
if (MessageBox.Show($"Are you sure you want to schedule {action} in just {Math.Ceiling((target - DateTime.Now).TotalSeconds)} seconds?", "Confirm", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
return true;
}
return false;
}
//warning threshold not met
return true;
}
public void shutdown(string timeString) public void shutdown(string timeString)
{ {
DateTime target; DateTime target;
@@ -116,6 +134,11 @@ namespace GoToSleep
throw; throw;
} }
if(!confirmDate(target,"shutdown"))
{
return;
}
using (var progressPopup = new CountdownForm(PowerDownData.Shutdown(target))) using (var progressPopup = new CountdownForm(PowerDownData.Shutdown(target)))
{ {
progressPopup.ShowDialog(parentForm); progressPopup.ShowDialog(parentForm);
@@ -134,6 +157,11 @@ namespace GoToSleep
throw; throw;
} }
if (!confirmDate(target, "suspension"))
{
return;
}
using (var progressPopup = new CountdownForm(PowerDownData.Suspend(target))) using (var progressPopup = new CountdownForm(PowerDownData.Suspend(target)))
{ {
progressPopup.ShowDialog(parentForm); progressPopup.ShowDialog(parentForm);
@@ -152,10 +180,38 @@ namespace GoToSleep
throw; throw;
} }
if (!confirmDate(target, "hibernation"))
{
return;
}
using (var progressPopup = new CountdownForm(PowerDownData.Hibernate(target))) using (var progressPopup = new CountdownForm(PowerDownData.Hibernate(target)))
{ {
progressPopup.ShowDialog(parentForm); progressPopup.ShowDialog(parentForm);
} }
} }
public void restart(string timeString)
{
DateTime target;
try
{
target = getTimeOffsetFromString(timeString);
}
catch (Exception)
{
throw;
}
if (!confirmDate(target, "restart"))
{
return;
}
using (var progressPopup = new CountdownForm(PowerDownData.Restart(target)))
{
progressPopup.ShowDialog(parentForm);
}
}
} }
} }
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) [year] [fullname] Copyright (c) 2026 Kamil118
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
+142
View File
@@ -0,0 +1,142 @@
License notice for Microsoft.Recognizers.Text (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for Microsoft.Recognizers.Text.DateTime (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for Microsoft.Extensions.Caching.Abstractions (v2.2.0)
------------------------------------
https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a
https://asp.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt
License notice for Microsoft.Extensions.Caching.Memory (v2.2.0)
------------------------------------
https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a
https://asp.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt
License notice for Microsoft.Extensions.DependencyInjection.Abstractions (v2.2.0)
------------------------------------
https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a
https://asp.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt
License notice for Microsoft.Extensions.Options (v2.2.0)
------------------------------------
https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a
https://asp.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt
License notice for Microsoft.Extensions.Primitives (v2.2.0)
------------------------------------
https://github.com/aspnet/Extensions at 9bc79b2f25a3724376d7af19617c33749a30ea3a
https://asp.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://raw.githubusercontent.com/aspnet/AspNetCore/2.0.0/LICENSE.txt
License notice for Microsoft.Recognizers.Definitions (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for Microsoft.Recognizers.Text.DataTypes.TimexExpression (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for Microsoft.Recognizers.Text.Number (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for Microsoft.Recognizers.Text.NumberWithUnit (v1.8.13)
------------------------------------
https://github.com/Microsoft/Recognizers-Text
Copyright © Microsoft Corporation. All rights reserved.
Licensed under MIT
Available at https://licenses.nuget.org/MIT
License notice for System.ComponentModel.Annotations (v4.5.0)
------------------------------------
https://dot.net/
Copyright © Microsoft Corporation. All rights reserved.
License available at https://github.com/dotnet/corefx/blob/master/LICENSE.TXT