Dodaj pliki projektów.

This commit is contained in:
Kamil committed 2026-08-17 13:25:47 +02:00
1 parent b73ed83b6c
commit 0b46f262e9
11 files changed
+912

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
<Solution>
<Project Path="GoToSleep/GoToSleep.csproj" />
</Solution>
+112
View File
@@ -0,0 +1,112 @@
namespace GoToSleep
{
partial class CountdownForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
countdown = new Label();
cancel = new Button();
execNow = new Button();
tableLayoutPanel1 = new TableLayoutPanel();
tableLayoutPanel1.SuspendLayout();
SuspendLayout();
//
// countdown
//
countdown.AutoSize = true;
tableLayoutPanel1.SetColumnSpan(countdown, 2);
countdown.Dock = DockStyle.Fill;
countdown.Font = new Font("Segoe UI", 27.75F, FontStyle.Regular, GraphicsUnit.Point, 238);
countdown.Location = new Point(3, 0);
countdown.Name = "countdown";
countdown.Size = new Size(478, 63);
countdown.TabIndex = 0;
countdown.Text = "Time Remaining: 99:99:99";
countdown.TextAlign = ContentAlignment.MiddleCenter;
countdown.Click += countdown_Click;
//
// cancel
//
cancel.Anchor = AnchorStyles.Left;
cancel.Location = new Point(245, 68);
cancel.Name = "cancel";
cancel.Size = new Size(75, 23);
cancel.TabIndex = 1;
cancel.Text = "Cancel";
cancel.UseVisualStyleBackColor = true;
cancel.Click += cancel_Click;
//
// execNow
//
execNow.Anchor = AnchorStyles.Right;
execNow.Location = new Point(164, 68);
execNow.Name = "execNow";
execNow.Size = new Size(75, 23);
execNow.TabIndex = 2;
execNow.Text = "Execute";
execNow.UseVisualStyleBackColor = true;
execNow.Click += execNow_Click;
//
// tableLayoutPanel1
//
tableLayoutPanel1.ColumnCount = 2;
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
tableLayoutPanel1.Controls.Add(countdown, 0, 0);
tableLayoutPanel1.Controls.Add(cancel, 1, 1);
tableLayoutPanel1.Controls.Add(execNow, 0, 1);
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.Location = new Point(0, 0);
tableLayoutPanel1.Name = "tableLayoutPanel1";
tableLayoutPanel1.RowCount = 2;
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 33F));
tableLayoutPanel1.Size = new Size(484, 96);
tableLayoutPanel1.TabIndex = 3;
//
// CountdownForm
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(484, 96);
Controls.Add(tableLayoutPanel1);
MinimumSize = new Size(500, 135);
Name = "CountdownForm";
Text = "CountdownForm";
tableLayoutPanel1.ResumeLayout(false);
tableLayoutPanel1.PerformLayout();
ResumeLayout(false);
}
#endregion
private Label countdown;
private Button cancel;
private Button execNow;
private TableLayoutPanel tableLayoutPanel1;
}
}
+99
View File
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace GoToSleep
{
public partial class CountdownForm : Form
{
public bool active = true;
private PowerDownData task;
private Thread taskThread;
public CountdownForm(PowerDownData taskData)
{
task = taskData;
InitializeComponent();
this.Text = task.actionType.ToString() + " Countdown";
countdown.Text = getTimerText();
taskThread = new Thread(threadFunction);
taskThread.Start();
}
string getTimerText()
{
var timeRemaining = task.when - DateTime.Now;
return $"Time Remaining: {(int)timeRemaining.TotalHours:D2}:{timeRemaining.Minutes:D2}:{timeRemaining.Seconds:D2}";
}
void updateTimer()
{
try
{
countdown.Invoke((MethodInvoker)delegate
{
countdown.Text = getTimerText();
});
}
catch (Exception e)
{
//the window most likely has been closed
return;
}
}
void threadFunction()
{
while (DateTime.Now < task.when)
{
Thread.Sleep(1000);
if (!active)
{
return;
}
updateTimer();
}
executeStateChange();
}
void executeStateChange()
{
active = false;
task.onCountdownEnd();
this.Invoke((MethodInvoker)delegate
{
this.Close();
});
}
private void execNow_Click(object sender, EventArgs e)
{
executeStateChange();
}
private void cancel_Click(object sender, EventArgs e)
{
active = false;
this.Close();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
active = false;
}
private void countdown_Click(object sender, EventArgs e)
{
}
}
}
+120
View File
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
+147
View File
@@ -0,0 +1,147 @@
namespace GoToSleep
{
partial class GoToSleep
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
comboBox1 = new ComboBox();
shutdownButton = new Button();
suspendButton = new Button();
hibernateButton = new Button();
textBox1 = new TextBox();
tableLayoutPanel2 = new TableLayoutPanel();
flowLayoutPanel1 = new FlowLayoutPanel();
tableLayoutPanel2.SuspendLayout();
flowLayoutPanel1.SuspendLayout();
SuspendLayout();
//
// comboBox1
//
comboBox1.FormattingEnabled = true;
comboBox1.Items.AddRange(new object[] { "In", "At" });
comboBox1.Location = new Point(3, 3);
comboBox1.MinimumSize = new Size(50, 0);
comboBox1.Name = "comboBox1";
comboBox1.Size = new Size(50, 23);
comboBox1.TabIndex = 0;
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
//
// shutdownButton
//
shutdownButton.Location = new Point(185, 3);
shutdownButton.Margin = new Padding(3, 3, 0, 3);
shutdownButton.Name = "shutdownButton";
shutdownButton.Size = new Size(75, 23);
shutdownButton.TabIndex = 1;
shutdownButton.Text = "Shutdown";
shutdownButton.UseVisualStyleBackColor = true;
shutdownButton.Click += shutdownButton_Click;
//
// suspendButton
//
suspendButton.Location = new Point(104, 3);
suspendButton.Name = "suspendButton";
suspendButton.Size = new Size(75, 23);
suspendButton.TabIndex = 2;
suspendButton.Text = "Suspend";
suspendButton.UseVisualStyleBackColor = true;
suspendButton.Click += suspendButton_Click;
//
// hibernateButton
//
hibernateButton.Location = new Point(23, 3);
hibernateButton.Name = "hibernateButton";
hibernateButton.Size = new Size(75, 23);
hibernateButton.TabIndex = 3;
hibernateButton.Text = "Hibernate";
hibernateButton.UseVisualStyleBackColor = true;
hibernateButton.Click += hibernateButton_Click;
//
// textBox1
//
textBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
textBox1.Location = new Point(59, 3);
textBox1.Name = "textBox1";
textBox1.Size = new Size(422, 23);
textBox1.TabIndex = 4;
//
// tableLayoutPanel2
//
tableLayoutPanel2.AutoSizeMode = AutoSizeMode.GrowAndShrink;
tableLayoutPanel2.ColumnCount = 2;
tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle());
tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle());
tableLayoutPanel2.Controls.Add(comboBox1, 0, 0);
tableLayoutPanel2.Controls.Add(textBox1, 1, 0);
tableLayoutPanel2.Controls.Add(flowLayoutPanel1, 1, 1);
tableLayoutPanel2.Dock = DockStyle.Fill;
tableLayoutPanel2.Location = new Point(0, 0);
tableLayoutPanel2.Name = "tableLayoutPanel2";
tableLayoutPanel2.RowCount = 2;
tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel2.RowStyles.Add(new RowStyle());
tableLayoutPanel2.Size = new Size(484, 64);
tableLayoutPanel2.TabIndex = 6;
//
// flowLayoutPanel1
//
flowLayoutPanel1.Anchor = AnchorStyles.Top | AnchorStyles.Right;
flowLayoutPanel1.Controls.Add(shutdownButton);
flowLayoutPanel1.Controls.Add(suspendButton);
flowLayoutPanel1.Controls.Add(hibernateButton);
flowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft;
flowLayoutPanel1.Location = new Point(221, 33);
flowLayoutPanel1.Name = "flowLayoutPanel1";
flowLayoutPanel1.Size = new Size(260, 41);
flowLayoutPanel1.TabIndex = 7;
//
// GoToSleep
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(484, 64);
Controls.Add(tableLayoutPanel2);
MinimumSize = new Size(500, 103);
Name = "GoToSleep";
Text = "Go To Sleep";
tableLayoutPanel2.ResumeLayout(false);
tableLayoutPanel2.PerformLayout();
flowLayoutPanel1.ResumeLayout(false);
ResumeLayout(false);
}
#endregion
private ComboBox comboBox1;
private Button shutdownButton;
private Button suspendButton;
private Button hibernateButton;
private TextBox textBox1;
private TableLayoutPanel tableLayoutPanel2;
private FlowLayoutPanel flowLayoutPanel1;
}
}
+73
View File
@@ -0,0 +1,73 @@
namespace GoToSleep
{
public partial class GoToSleep : Form
{
string timeSelector = "in";
SystemPowerManager manager;
public GoToSleep()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
textBox1.Select();
manager = new SystemPowerManager(this);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 0:
timeSelector = "in";
break;
case 1:
timeSelector = "";
break;
default:
//this should never happen, something went very wrong, the combo box should only have 2 options
throw new Exception("Invalid selection index");
}
}
private string getTimeString()
{
return timeSelector + " " + textBox1.Text;
}
private void shutdownButton_Click(object sender, EventArgs e)
{
try
{
manager.shutdown(getTimeString());
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
private void suspendButton_Click(object sender, EventArgs e)
{
try
{
manager.suspend(getTimeString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void hibernateButton_Click(object sender, EventArgs e)
{
try
{
manager.hibernate(getTimeString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
+17
View File
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Chronic.Signed" Version="0.3.2" />
<PackageReference Include="Microsoft.Recognizers.Text" Version="1.8.13" />
<PackageReference Include="Microsoft.Recognizers.Text.DateTime" Version="1.8.13" />
</ItemGroup>
</Project>
+120
View File
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
+54
View File
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace GoToSleep
{
public class PowerDownData
{
public enum PowerDownType
{
Shutdown,
Suspend,
Hibernate
};
public PowerDownType actionType { get; }
public DateTime when { get; }
public delegate void Action();
public Action onCountdownEnd { get; }
private PowerDownData(PowerDownType _type, DateTime _when, Action action)
{
actionType = _type;
when = _when;
onCountdownEnd = action;
}
public static PowerDownData Shutdown(DateTime when)
{
return new PowerDownData(PowerDownType.Shutdown, when, () => {
var psi = new ProcessStartInfo("shutdown", "/s /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
});
}
public static PowerDownData Suspend(DateTime when)
{
return new PowerDownData(PowerDownType.Suspend, when, () => {
Application.SetSuspendState(PowerState.Suspend, false, false);
});
}
public static PowerDownData Hibernate(DateTime when)
{
return new PowerDownData(PowerDownType.Hibernate, when, () => {
Application.SetSuspendState(PowerState.Hibernate, false, false);
});
}
}
}
+17
View File
@@ -0,0 +1,17 @@
namespace GoToSleep
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new GoToSleep());
}
}
}
+150
View File
@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms.Design;
using Chronic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Microsoft.Recognizers.Text;
using Microsoft.Recognizers.Text.DateTime;
using System.Globalization;
namespace GoToSleep
{
internal class SystemPowerManager
{
[Serializable]
class DatetimeStringException : Exception
{
public DatetimeStringException(string message) : base(message) { }
public DatetimeStringException(Exception e) : base("Conversion failed.", e) { }
}
[Serializable]
class DateTimeBeforeException : DatetimeStringException
{
public DateTimeBeforeException() : base("The entered date was in the past.")
{
}
}
[Serializable]
class ParseFailedException : DatetimeStringException
{
public ParseFailedException(Exception e) : base(e) { }
}
public SystemPowerManager(GoToSleep pf)
{
parentForm = pf;
}
Form parentForm;
Chronic.Parser parser = new Chronic.Parser();
string prepareTimeString(string s)
{
// make sure there are spaces between numbers and units, for the parser
s = Regex.Replace(s, "([0-9]+)([a-zA-Z]+)", "$1 $2");
s = Regex.Replace(s, "([a-zA-Z]+)([0-9]+)", "$1 $2");
// replace shorthands with full names, for the parser
s = Regex.Replace(s, "( )[hH]([ ]|$)", "$1hours$2");
s = Regex.Replace(s, "( )[mM]([ ]|$)", "$1minutes$2");
s = Regex.Replace(s, "( )[sS]([ ]|$)", "$1seconds$2");
return s;
}
DateTime getTimeOffsetFromString(string timeString)
{
DateTime result;
try
{
timeString = prepareTimeString(timeString);
//var tmp = parser.Parse(timeString);
var results = DateTimeRecognizer.RecognizeDateTime(timeString, CultureInfo.CurrentCulture.ToString());
if (results == null || results.Count == 0)
{
results = DateTimeRecognizer.RecognizeDateTime(timeString, Culture.English);
if (results == null || results.Count == 0)
{
throw new ArgumentException();
}
}
result = DateTime.Parse((results[0].Resolution["values"] as List<Dictionary<string, string>>)[0]["value"]);
}
catch (Exception e)
{
throw new ParseFailedException(e);
}
if (result < DateTime.Now)
{
throw new DateTimeBeforeException();
}
return result;
}
public void shutdown(string timeString)
{
DateTime target;
try
{
target = getTimeOffsetFromString(timeString);
}
catch (Exception)
{
throw;
}
using (var progressPopup = new CountdownForm(PowerDownData.Shutdown(target)))
{
progressPopup.ShowDialog(parentForm);
}
}
public void suspend(string timeString)
{
DateTime target;
try
{
target = getTimeOffsetFromString(timeString);
}
catch (Exception)
{
throw;
}
using (var progressPopup = new CountdownForm(PowerDownData.Suspend(target)))
{
progressPopup.ShowDialog(parentForm);
}
}
public void hibernate(string timeString)
{
DateTime target;
try
{
target = getTimeOffsetFromString(timeString);
}
catch (Exception)
{
throw;
}
using (var progressPopup = new CountdownForm(PowerDownData.Hibernate(target)))
{
progressPopup.ShowDialog(parentForm);
}
}
}
}