-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWatchAndWait.cs
447 lines (399 loc) · 14.9 KB
/
WatchAndWait.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Sandman;
public static class WatchAndWait
{
// Saving the window reference ensures this static class won't go out of scope and the event handler removed.
private static MainWindow? TheWindow;
private static readonly string StayAwakeFile = Path.ChangeExtension(Environment.MachineName, "txt");
/// <summary>
/// If the user has been active recently, wait again.
/// Short pause (for processes to finish activities).
/// Sleep computer.
/// </summary>
public static async Task StartAsync(MainWindow theWindow)
{
if (TheWindow is not null)
{
throw new ArgumentException($"{nameof(StartAsync)} should be called only once.");
}
TheWindow = theWindow;
TheWindow.WriteInformation($"Note: You can create a file named \"{StayAwakeFile}\" in this folder to stay awake while the file exists.");
// Handle resuming from sleep.
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
// It's a static event, so we have to remove the handler when our app is disposed to avoid memory leaks.
AppDomain.CurrentDomain.ProcessExit += (object? senderExit, EventArgs eExit) =>
{
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;
};
await TrySleepComputerAsync(TimeSpan.Zero).ConfigureAwait(continueOnCapturedContext: false);
}
private static async void SystemEvents_PowerModeChanged(object? sender, PowerModeChangedEventArgs e)
{
ArgumentNullException.ThrowIfNull(TheWindow);
switch (e.Mode)
{
case PowerModes.Resume:
{
TheWindow.WriteInformation(string.Empty);
TheWindow.WriteInformation($"PowerMode: {e.Mode}");
await TrySleepComputerAsync(Properties.Settings.Default.DelayAfterResume).ConfigureAwait(continueOnCapturedContext: false);
break;
}
case PowerModes.Suspend:
case PowerModes.StatusChange:
default:
{
break;
}
}
}
/// <summary>
/// Check if we can sleep the computer.
/// </summary>
/// <remarks>
/// This is called when:
/// - the application starts
/// - the computer resumes from standby
/// - the UI restarts the wait
/// </remarks>
/// <returns>True if we put the computer to sleep.</returns>
private static CancellationTokenSource? TokenSource { get; set; } = null;
private static ManualResetEventSlim TrySleepComplete { get; set; } = new ManualResetEventSlim();
public static async Task RestartWaitAsync() => await TrySleepComputerAsync(TimeSpan.Zero).ConfigureAwait(continueOnCapturedContext: false);
private static async Task TrySleepComputerAsync(TimeSpan initialDelay)
{
ArgumentNullException.ThrowIfNull(TheWindow);
await CancelWaitTaskAsync();
Debug.Assert(TokenSource is null);
TheWindow.WriteInformation(string.Empty);
TheWindow.WriteInformation("Checking if we can sleep...");
CancellationTokenSource localTokenSource = new();
TrySleepComplete.Reset();
TokenSource = localTokenSource; // "publish" a reference for other threads to use to cancel this one
// Switch to background to do the wait
await Shared.ThreadSwitcher.ResumeBackgroundAsync();
bool bManualRestart = false;
try
{
// If we delay before sleeping, we need to be able to cancel it.
if (initialDelay > TimeSpan.Zero)
{
TheWindow.WriteInformation($"Initial delay for {initialDelay.TotalMinutes:N2} minutes...");
await Task.Delay(initialDelay, localTokenSource.Token);
}
if (!await WaitUntilCanSleepComputerAsync(localTokenSource.Token))
{
// Stop checking
return;
}
// Wait a bit in case something needs to finish.
if (Properties.Settings.Default.DelayBeforeSleep > TimeSpan.Zero)
{
TheWindow.WriteInformation($"Delaying {Properties.Settings.Default.DelayBeforeSleep.TotalSeconds:N0} seconds before sleeping...");
await Task.Delay(Properties.Settings.Default.DelayBeforeSleep, localTokenSource.Token);
}
bManualRestart = !SleepComputer();
}
catch (TaskCanceledException ex) // From CancellationToken.Cancel()
{
TheWindow.WriteInformation($"{ex.GetType()}: {ex.Message}--stop checking.");
return;
}
catch (OperationCanceledException ex) // From CancellationTokenRegistration?
{
TheWindow.WriteInformation($"{ex.GetType()}: {ex.Message}--stop checking.");
return;
}
finally
{
/// We need to signal we're complete--whether it's RanToCompletion
/// or Canceled--so that the above wait for completion will succeed.
TrySleepComplete.Set();
}
/// Note: We have to do this AFTER the above `finally` so that the event is set.
/// Then, when we try to sleep again, the wait for the event will succeed (not block).
#if DEBUG
TheWindow.WriteWarning("DEBUG: Skipped sleeping computer. Simulate resuming...");
/// Simulate resuming (also give us time to set the <see cref="TrySleepComplete"/> event.
await TrySleepComputerAsync(Properties.Settings.Default.DelayAfterResume).ConfigureAwait(continueOnCapturedContext: false);
#else
if (bManualRestart)
{
// Sleep failed, so we must restart the wait manually.
await TrySleepComputerAsync(TimeSpan.Zero).ConfigureAwait(continueOnCapturedContext: false);
}
#endif
}
/// <summary>
/// Cancel the task waiting until it can sleep the computer.
/// </summary>
/// <remarks>This must be called before the application exits.</remarks>
public static async Task CancelWaitTaskAsync()
{
if (TokenSource is null)
{
return;
}
ArgumentNullException.ThrowIfNull(TheWindow);
/// It's possible to get, for example, a WMC "recording finished" event while
/// we're waiting for "user activity" task. So we need to cancel an extant wait.
TokenSource.Cancel();
// Wait for task to complete cancellation and signal.
await Shared.ThreadSwitcher.ResumeBackgroundAsync();
TrySleepComplete.Wait();
await Shared.ThreadSwitcher.ResumeForegroundAsync(TheWindow.Dispatcher);
TokenSource.Dispose();
TokenSource = null;
}
/// <summary>
/// Test the required conditions for sleeping the computer. If they're
/// not present, keep trying until we're canceled or should stop.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns>True if computer can sleep; false if we are to stop checking</returns>
private enum EOnCompletion
{
CheckAgain,
StopChecking,
SleepComputer,
}
private static async Task<bool> WaitUntilCanSleepComputerAsync(CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(TheWindow);
EOnCompletion nextStep;
do
{
nextStep = await CanSleepComputer(cancellationToken);
TheWindow.WriteInformation($"Done awaiting. [{nextStep}]");
} while (nextStep == EOnCompletion.CheckAgain);
Debug.Assert((nextStep == EOnCompletion.StopChecking) || (nextStep == EOnCompletion.SleepComputer));
return (nextStep == EOnCompletion.SleepComputer);
}
/// <summary>
/// Determine if we should sleep the computer.
/// If we can't, return a task we can wait for (and then check again).
/// </summary>
/// <remarks>
/// To determine if it was canceled, the caller must check the token.
/// This method does not await the returned task, so it cannot catch a
/// TaskCanceledException or OperationCanceledException--whatever awaits must do that.
/// </remarks>
/// <param name="cancellationToken"></param>
/// <exception cref="TaskCanceledException"/>
/// <returns>
/// Task to wait for. (It may already be completed.)
/// The task's result can be: Check again, Stop checking, or sleep computer.
/// </returns>
private static Task<EOnCompletion> CanSleepComputer(CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(TheWindow);
///
/// If any of these are happening, we create a task to wait for.
/// Else, we return a completed task with the desired result.
///
///
/// Not if the stay-awake file ("{machine-name}.txt") is present
///
/// if file is present, wait for it to be deleted (or canceled).
if (File.Exists(Path.Combine(MainWindow.ExecutableFolder, StayAwakeFile)))
{
TheWindow.WriteInformation("Stay-awake file exists--staying awake.");
return StayAwakeAsync(StayAwakeFile, cancellationToken)
.ContinueWith((Task completed) => EOnCompletion.CheckAgain, TaskContinuationOptions.NotOnCanceled);
}
///
/// Not if user has been active
///
var remainingTime = Properties.Settings.Default.TimeUserInactiveBeforeSleep - NativeMethods.GetTimeSinceLastActivity();
#if DEBUG
TheWindow.WriteWarning($"DEBUG: Changing user-activity timeout to 5 seconds (instead of {remainingTime.TotalMinutes:N2} minutes).");
remainingTime = TimeSpan.FromSeconds(5) - NativeMethods.GetTimeSinceLastActivity();
#endif
if (remainingTime > TimeSpan.Zero)
{
TheWindow.WriteInformation($"User is active--staying awake for {remainingTime.TotalMinutes:N2} minutes...");
return Task.Delay(remainingTime, cancellationToken)
.ContinueWith((Task completed) => EOnCompletion.CheckAgain, TaskContinuationOptions.NotOnCanceled);
}
///
/// Not if certain processes are open
///
var processNames = Properties.Settings.Default.BlockingProcesses.Split(';');
var runningProcesses = GetRunningProcesses(processNames);
Task? runningTask = GetRunningProcessTask(runningProcesses, cancellationToken);
if (runningTask is not null)
{
Debug.Assert(runningProcesses.Count > 0);
TheWindow.WriteInformation($"Blocking process{((runningProcesses.Count == 1) ? string.Empty : "(es)")} [{string.Join(", ", runningProcesses.Select(p => p.Name))}] {((runningProcesses.Count == 1) ? "is" : "are")} open--staying awake.");
return runningTask
.ContinueWith((Task completed) => EOnCompletion.CheckAgain, TaskContinuationOptions.NotOnCanceled);
}
// There are no blocking processes, so continue.
Debug.Assert(!cancellationToken.IsCancellationRequested, "If cancellation was requested, we should have thrown already.");
cancellationToken.ThrowIfCancellationRequested(); // just in case
TheWindow.WriteInformation("No activity is blocking.");
return Task.FromResult(EOnCompletion.SleepComputer);
}
/// <summary>
/// Waits for the passed file to be deleted.
/// </summary>
/// <remarks>
/// This does not work on a virtual machine's shared folder because it
/// seems not to receive any notifications of changes.
///
/// We could probably do this instead, but it's less clean:
///
/// Task<EOnCompletion> t = Task.Run(() => watcher.WaitForChanged(WatcherChangeTypes.Deleted), cancellationToken)
/// .ContinueWith((Task<WaitForChangedResult> t1) => EOnCompletion.CheckAgain);
/// if (t.IsCanceled)
/// {
/// }
/// </remarks>
/// <param name="cancellationToken"></param>
private static async Task StayAwakeAsync(string stayAwakeFile, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(TheWindow);
TaskCompletionSource<object?> processComplete = new(TaskCreationOptions.RunContinuationsAsynchronously);
FileSystemWatcher? watcher = CreateFileSystemWatcher(MainWindow.ExecutableFolder, stayAwakeFile);
if (watcher is null)
{
TheWindow.WriteError("Unable to watch stay-awake file.");
return;
}
watcher.Created += (object sender, FileSystemEventArgs e) =>
{
TheWindow.WriteInformation("Stay-awake file has been created.");
processComplete.TrySetResult(null);
};
watcher.Renamed += (object sender, RenamedEventArgs e) =>
{
TheWindow.WriteInformation("Stay-awake file has been renamed.");
processComplete.TrySetResult(null);
};
watcher.Deleted += (object sender, FileSystemEventArgs e) =>
{
TheWindow.WriteInformation("Stay-awake file has been deleted.");
processComplete.TrySetResult(null);
};
watcher.EnableRaisingEvents = true;
using (CancellationTokenRegistration registration = cancellationToken.Register(state =>
{
if (state is null)
{
return;
}
TheWindow.WriteInformation($"{nameof(CancellationTokenRegistration)}: Stay-awake wait canceled.");
FileSystemWatcher w = (FileSystemWatcher)state;
w.EnableRaisingEvents = false;
processComplete.TrySetCanceled();
}, watcher)
)
{
try
{
await processComplete.Task;
}
// We need this because the task might be canceled.
finally
{
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}
}
}
public static FileSystemWatcher? CreateFileSystemWatcher(string path, string filter)
{
try
{
return new FileSystemWatcher(path, filter)
{
IncludeSubdirectories = false,
NotifyFilter = NotifyFilters.FileName
};
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Get collection of blacklisted processes that are running.
/// </summary>
/// <remarks>We dispose the ones we don't return.</remarks>
/// <param name="processNames"></param>
/// <returns>Collection of running processes with the passed names</returns>
private static List<Shared.SafeProcess> GetRunningProcesses(string[] processNames)
{
return processNames
.SelectMany(name => Process.GetProcessesByName(name))
.Select(process => new Shared.SafeProcess(process))
.Where(safeProcess =>
{
if (safeProcess.SafeHasExited())
{
safeProcess.Dispose();
return false;
}
return true;
})
.ToList();
}
/// <summary>
/// Return a task representing one of the running processes.
/// Returns null if there are no such processes (and we can continue).
/// </summary>
/// <remarks>
/// We don't need to wait for ALL the processes at once.
/// We can wait for them one at a time.
/// We prefer normal (non-elevated) processes because we cannot
/// wait for processes with administrator privileges to exit.
/// Note that the returned Process objects must be disposed.
/// </remarks>
/// <param name="processes">Set of blacklisted processes that are running</param>
/// <returns>
/// Task representing one of the blocking processes which will complete when the process exits.
/// If the wait is canceled, it will throw.
/// </returns>
private static Task? GetRunningProcessTask(List<Shared.SafeProcess> processes, CancellationToken cancellationToken)
{
// Dispose of the other Process objects.
foreach (var p in processes.OrderBy(process => process.SafeIsElevated).Skip(1))
{
p.Dispose();
}
return processes
.FirstOrDefault()?
.SafeWaitAsync(Properties.Settings.Default.DelayForElevatedProcess, cancellationToken);
}
/// <summary>
/// Put the computer in sleep mode.
/// </summary>
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.setsuspendstate"/>
/// <returns>True if sleeping computer is successful; false if not.</returns>
private static bool SleepComputer()
{
ArgumentNullException.ThrowIfNull(TheWindow);
TheWindow.WriteInformation("Sleeping...");
#if DEBUG
bool ok = true;
#else
bool ok = System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Suspend, force: false, disableWakeEvent: false);
#endif
if (ok)
{
TheWindow.WriteInformation("Success.");
}
else
{
TheWindow.WriteError("Failure.");
}
return ok;
}
}