-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StandbyEventEmitter.cs
61 lines (48 loc) · 1.97 KB
/
StandbyEventEmitter.cs
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
using System.Diagnostics.Eventing.Reader;
namespace PowerMateVolume;
public interface IStandbyListener: IDisposable {
event EventHandler StandingBy;
event EventHandler Resumed;
event EventHandler<Exception> FatalError;
}
public class EventLogStandbyListener: IStandbyListener {
public event EventHandler? StandingBy;
public event EventHandler? Resumed;
public event EventHandler<Exception>? FatalError;
private readonly EventLogWatcher _logWatcher;
/// <exception cref="EventLogNotFoundException">if the given event log or file was not found</exception>
/// <exception cref="UnauthorizedAccessException">if the log did not already exist and this program is not running elevated</exception>
public EventLogStandbyListener() {
_logWatcher = new EventLogWatcher(new EventLogQuery("System", PathType.LogName, "*[System[Provider/@Name=\"Microsoft-Windows-Kernel-Power\" and (EventID=42 or EventID=107)]]"));
_logWatcher.EventRecordWritten += onEventRecord;
try {
_logWatcher.Enabled = true;
} catch (EventLogNotFoundException) {
_logWatcher.Dispose();
throw;
} catch (UnauthorizedAccessException) {
_logWatcher.Dispose();
throw;
}
}
private void onEventRecord(object? sender, EventRecordWrittenEventArgs e) {
if (e.EventException is { } exception) {
FatalError?.Invoke(this, exception);
Dispose();
} else {
using EventRecord? record = e.EventRecord;
switch (record?.Id) {
case 42:
StandingBy?.Invoke(this, EventArgs.Empty);
break;
case 107:
Resumed?.Invoke(this, EventArgs.Empty);
break;
}
}
}
public void Dispose() {
_logWatcher.Dispose();
GC.SuppressFinalize(this);
}
}