-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
143 lines (122 loc) · 5.5 KB
/
Program.cs
File metadata and controls
143 lines (122 loc) · 5.5 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
using ForceModernStandby;
using PowerNotificationMonitor;
using System.Diagnostics;
using System.Runtime.InteropServices;
class Program
{
static bool _powerRestored = false;
static bool _suspendInProgress = false;
static bool _hasBeenSuspended = false;
[STAThread]
static async Task Main(string[] args)
{
object objLock = new object();
try
{
using RadioManager radioManager = new RadioManager();
using var powerMonitor = new PowerMonitor();
powerMonitor.MonitorPowerChanged += async (sender, e) =>
{
bool shouldRestore = false;
lock (objLock)
{
if (_powerRestored || _suspendInProgress || !_hasBeenSuspended) return; // Only restore if we've actually been suspended
//Console.Beep();
//Console.WriteLine($"Monitor power setting changed: {e.State}");
FlightModeState currentState = radioManager.GetFlightModeState();
//Console.WriteLine($"Current flight mode is: {(currentState == FlightModeState.Enabled ? "on" : "off")}");
if (currentState == FlightModeState.Enabled)
{
Console.WriteLine("***************************************************");
Console.WriteLine("Power restored - beginning network restoration...");
Console.WriteLine("***************************************************");
shouldRestore = true;
}
}
// Move heavy operations outside the lock and add delays
if (shouldRestore)
{
await Task.Run(async () =>
{
try
{
Console.Beep();
// Add delay before network operations
await Task.Delay(1000);
NetworkAdapterManager.EnableAllNetworkAdapters();
// Add delay before flight mode change
await Task.Delay(500);
radioManager.SetFlightModeState(FlightModeState.Disabled);
// Verify state change
await Task.Delay(200);
Console.WriteLine($"Flight mode has been turned {(radioManager.GetFlightModeState() == FlightModeState.Enabled ? "on" : "off")}.");
Console.WriteLine("All operations complete.");
lock (objLock)
{
_powerRestored = true;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error during power restoration: {ex.Message}");
}
});
}
};
radioManager.SetFlightModeState(FlightModeState.Enabled);
await Task.Delay(500); // Let flight mode stabilize
Console.WriteLine($"Flight mode has been turned {(radioManager.GetFlightModeState() == FlightModeState.Enabled ? "on" : "off")}.");
await Task.Delay(300);
NetworkAdapterManager.DisableAllNetworkAdapters();
await Task.Delay(800); // Let network adapters fully disable
// int killedProcesses = ProcessKiller.KillDefaultProcesses();
// if (killedProcesses > 0)
// {
// await Task.Delay(500); // Give killed processes time to clean up
// }
//await Task.Delay(200);
// Set suspend flag to prevent power event handler from firing during sleep initiation
lock (objLock)
{
_suspendInProgress = true;
_hasBeenSuspended = true; // Mark that we've attempted suspend
}
Console.WriteLine("Killing problematic processes before sleep...");
ProcessKiller.KillDefaultProcesses();
Console.WriteLine("Putting computer into modern standby. Please wait.");
SleepManager.ModernStandbySleepWorkaround();
// Wait a bit to allow system to settle, then clear suspend flag
await Task.Delay(3000);
lock (objLock)
{
_suspendInProgress = false;
}
//await Task.Delay(2000);
Console.WriteLine("Press Ctrl+C to exit if no response.");
while (!_powerRestored)
{
await Task.Delay(500);
}
//Console.WriteLine("Press return to exit.");
Console.ReadLine();
}
catch (RadioManagerException ex)
{
Console.WriteLine($"RadioManager Error: {ex.Message} (HRESULT: 0x{ex.HResultCode:X})");
Console.WriteLine("Press return key to exit");
Console.ReadLine();
}
catch (COMException comEx)
{
Console.WriteLine($"COM Exception: {comEx.Message} (HRESULT: 0x{comEx.ErrorCode:X})");
Console.WriteLine("Press return key to exit");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
Console.WriteLine("Press return key to exit");
Console.ReadLine();
}
}
}