-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (135 loc) · 5.75 KB
/
Program.cs
File metadata and controls
157 lines (135 loc) · 5.75 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
namespace TimeMonitorInvisibleAdminConsole
{
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Environment;
using static System.IO.File;
using static System.Threading.Tasks.Task;
internal class Program
{
public static Stopwatch swTimer = new Stopwatch();
private static async Task Main()
{
swTimer.Start();
await DetectPowerChange();
await GetCurrentSessionTimes();
}
static async Task DetectPowerChange()
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerChangeAsync);
await Delay(1000);
}
public static String GetElapsedTime(TimeSpan e)
{
String elapsed = "";
if (e.TotalMilliseconds < 1000)
{
elapsed = e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalSeconds < 60 && e.TotalMilliseconds >= 1000)
{
elapsed = e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalMinutes < 60 && e.TotalSeconds >= 60)
{
elapsed = e.Minutes + " m " + e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalHours < 24 && e.TotalMinutes >= 60)
{
elapsed = e.Hours + " h " + e.Minutes + " m " + e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalDays < 7 && e.TotalHours >= 24)
{
elapsed = e.Days + " d " + e.Hours + " h " + e.Minutes + " m " + e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalDays >= 7)
{
elapsed = (e.Days / 7) + " w " + e.Days + " d " + e.Hours + " h " + e.Minutes + " m " + e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
if (e.TotalDays >= 365)
{
elapsed = (e.TotalDays / 365) + " y " + (e.Days / 7) + " w " + e.Days + " d " + e.Hours + " h " + e.Minutes + " m " + e.Seconds + " s " + e.Milliseconds + " ms";
return elapsed;
}
return elapsed;
}
public static async void OnPowerChangeAsync(Object s, PowerModeChangedEventArgs e)
{
StringBuilder sbPower = new StringBuilder();
if (e.Mode == PowerModes.Resume)
{
sbPower.AppendLine($"Suspended for: {GetElapsedTime(swTimer.Elapsed)}");
sbPower.AppendLine($"At: {DateTime.Now}");
sbPower.AppendLine();
sbPower.AppendLine();
await Delay(1000);
swTimer.Restart();
}
if (e.Mode == PowerModes.Suspend)
{
sbPower.AppendLine($"Resumed for: {GetElapsedTime(swTimer.Elapsed)}");
sbPower.AppendLine($"At: {DateTime.Now}");
sbPower.AppendLine();
sbPower.AppendLine();
await Delay(1000);
swTimer.Restart();
}
AppendAllText($@"{GetLogicalDrives()[0]}UsageTimesLog.txt", sbPower.ToString());
await Delay(1000);
}
public static async Task GetCurrentSessionTimes()
{
while (true)
{
StringBuilder sbTimer = new StringBuilder();
BigInteger ns = (BigInteger)TickCount * 1000000;
BigInteger ohnsu = (BigInteger)TickCount * 10000;
BigInteger ms = TickCount;
BigInteger s = (BigInteger)TickCount / 1000;
BigInteger min = (BigInteger)TickCount / 60000;
BigInteger h = (BigInteger)TickCount / 3600000;
BigInteger d = (BigInteger)TickCount / 86400000;
BigInteger w = (BigInteger)TickCount / 604800000;
BigInteger f = (BigInteger)TickCount / 1209600000;
BigInteger mon = (BigInteger)TickCount / 2629800000;
BigInteger y = (BigInteger)TickCount / 31557600000;
TimeSpan ts = new TimeSpan((Int64)((BigInteger)TickCount * 10000));
sbTimer.AppendLine("Current session:");
sbTimer.AppendLine();
sbTimer.AppendLine(GetElapsedTime(swTimer.Elapsed));
sbTimer.AppendLine();
sbTimer.AppendLine();
sbTimer.AppendLine("Time since last restart:");
sbTimer.AppendLine();
sbTimer.AppendLine(GetElapsedTime(ts));
sbTimer.AppendLine();
sbTimer.AppendLine(ns + " nanoseconds");
sbTimer.AppendLine(ohnsu + " 100-nanosecond units");
sbTimer.AppendLine(ms + " milliseconds");
sbTimer.AppendLine(s + " seconds");
sbTimer.AppendLine(min + " minutes");
sbTimer.AppendLine(h + " hours");
sbTimer.AppendLine(d + " days");
sbTimer.AppendLine(w + " weeks");
sbTimer.AppendLine(f + " fortnights");
sbTimer.AppendLine(mon + " months");
sbTimer.AppendLine(y + " years");
sbTimer.AppendLine();
sbTimer.AppendLine();
WriteAllText($@"{GetLogicalDrives()[0]}TimerElapsed.txt", sbTimer.ToString());
sbTimer.Clear();
await Delay(10000);
}
}
}
}