-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
115 lines (106 loc) · 3.58 KB
/
MainForm.cs
File metadata and controls
115 lines (106 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
namespace AutoShutdown
{
public partial class MainForm : Form
{
private DateTime? _scheduledAt;
private int _scheduledSeconds;
public MainForm()
{
InitializeComponent();
dtpSchedule.Value = DateTime.Now.AddMinutes(5);
}
private void btnSchedule_Click(object sender, EventArgs e)
{
var target = dtpSchedule.Value;
var now = DateTime.Now;
if (target <= now)
{
MessageBox.Show("Please choose a future time.", "Invalid Time", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
var seconds = (int)Math.Round((target - now).TotalSeconds);
ScheduleShutdown(seconds);
_scheduledAt = target;
_scheduledSeconds = seconds;
timerUpdate.Start();
}
private void btnCountdown_Click(object sender, EventArgs e)
{
int seconds = (int)txtSeconds.Value;
ScheduleShutdown(seconds);
_scheduledAt = DateTime.Now.AddSeconds(seconds);
_scheduledSeconds = seconds;
timerUpdate.Start();
}
private void btnCancel_Click(object sender, EventArgs e)
{
try
{
RunShutdownArgs("/a");
lblStatus.Text = "Canceled any scheduled shutdowns.";
_scheduledAt = null;
timerUpdate.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Cancel failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void timerUpdate_Tick(object sender, EventArgs e)
{
if (_scheduledAt.HasValue)
{
var remaining = _scheduledAt.Value - DateTime.Now;
if (remaining.TotalSeconds <= 0)
{
lblStatus.Text = "Shutdown imminent...";
timerUpdate.Stop();
}
else
{
lblStatus.Text = $"Shutdown scheduled at {_scheduledAt:yyyy-MM-dd HH:mm:ss} (in {FormatSpan(remaining)}).";
}
}
else
{
lblStatus.Text = "Ready.";
}
}
private void ScheduleShutdown(int seconds)
{
try
{
var sb = new StringBuilder();
sb.Append("/s /t ").Append(seconds);
if (chkForce.Checked) sb.Append(" /f");
RunShutdownArgs(sb.ToString());
lblStatus.Text = $"Scheduled shutdown in {seconds} seconds.";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Schedule failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static string FormatSpan(TimeSpan ts)
{
if (ts.TotalHours >= 1)
return $"{(int)ts.TotalHours}h {ts.Minutes}m {ts.Seconds}s";
if (ts.TotalMinutes >= 1)
return $"{(int)ts.TotalMinutes}m {ts.Seconds}s";
return $"{(int)Math.Max(0, Math.Round(ts.TotalSeconds))}s";
}
private static void RunShutdownArgs(string args)
{
var psi = new ProcessStartInfo("shutdown", args)
{
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(psi);
}
}
}