| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Collections.Specialized; |
|---|
| 4 | using System.Text; |
|---|
| 5 | using System.Threading; |
|---|
| 6 | using System.IO; |
|---|
| 7 | |
|---|
| 8 | using Eraser.Util; |
|---|
| 9 | using System.Runtime.Serialization; |
|---|
| 10 | using System.Runtime.Serialization.Formatters.Binary; |
|---|
| 11 | |
|---|
| 12 | namespace Eraser.Manager |
|---|
| 13 | { |
|---|
| 14 | /// <summary> |
|---|
| 15 | /// The DirectExecutor class is used by the Eraser GUI directly when the program |
|---|
| 16 | /// is run without the help of a Service. |
|---|
| 17 | /// </summary> |
|---|
| 18 | public class DirectExecutor : Executor, IDisposable |
|---|
| 19 | { |
|---|
| 20 | public DirectExecutor() |
|---|
| 21 | { |
|---|
| 22 | thread = new Thread(delegate() |
|---|
| 23 | { |
|---|
| 24 | Main(); |
|---|
| 25 | }); |
|---|
| 26 | |
|---|
| 27 | thread.Start(); |
|---|
| 28 | Thread.Sleep(0); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | void IDisposable.Dispose() |
|---|
| 32 | { |
|---|
| 33 | thread.Abort(); |
|---|
| 34 | schedulerInterrupt.Set(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | public override void AddTask(ref Task task) |
|---|
| 38 | { |
|---|
| 39 | lock (unusedIdsLock) |
|---|
| 40 | { |
|---|
| 41 | if (unusedIds.Count != 0) |
|---|
| 42 | { |
|---|
| 43 | task.id = unusedIds[0]; |
|---|
| 44 | unusedIds.RemoveAt(0); |
|---|
| 45 | } |
|---|
| 46 | else |
|---|
| 47 | task.id = ++nextId; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | //Set the executor of the task |
|---|
| 51 | task.executor = this; |
|---|
| 52 | |
|---|
| 53 | //Add the task to the set of tasks |
|---|
| 54 | lock (tasksLock) |
|---|
| 55 | { |
|---|
| 56 | tasks.Add(task.ID, task); |
|---|
| 57 | |
|---|
| 58 | //If the task is scheduled to run now, break the waiting thread and |
|---|
| 59 | //run it immediately |
|---|
| 60 | if (task.Schedule == Schedule.RunNow) |
|---|
| 61 | { |
|---|
| 62 | QueueTask(task); |
|---|
| 63 | } |
|---|
| 64 | //If the task is scheduled, add the next execution time to the list |
|---|
| 65 | //of schduled tasks. |
|---|
| 66 | else if (task.Schedule != Schedule.RunOnRestart) |
|---|
| 67 | { |
|---|
| 68 | scheduledTasks.Add((task.Schedule as RecurringSchedule).NextRun, task); |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public override bool DeleteTask(uint taskId) |
|---|
| 74 | { |
|---|
| 75 | lock (tasksLock) |
|---|
| 76 | { |
|---|
| 77 | if (!tasks.ContainsKey(taskId)) |
|---|
| 78 | return false; |
|---|
| 79 | |
|---|
| 80 | lock (unusedIdsLock) |
|---|
| 81 | unusedIds.Add(taskId); |
|---|
| 82 | tasks.Remove(taskId); |
|---|
| 83 | |
|---|
| 84 | for (int i = 0; i != scheduledTasks.Count; ++i) |
|---|
| 85 | if (scheduledTasks.Values[i].id == taskId) |
|---|
| 86 | scheduledTasks.RemoveAt(i); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return true; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public override void ReplaceTask(Task task) |
|---|
| 93 | { |
|---|
| 94 | lock (tasksLock) |
|---|
| 95 | { |
|---|
| 96 | //Replace the task in the global set |
|---|
| 97 | if (!tasks.ContainsKey(task.ID)) |
|---|
| 98 | return; |
|---|
| 99 | |
|---|
| 100 | tasks[task.ID] = task; |
|---|
| 101 | |
|---|
| 102 | //Then replace the task if it is in the queue |
|---|
| 103 | for (int i = 0; i != scheduledTasks.Count; ++i) |
|---|
| 104 | if (scheduledTasks.Values[i].id == task.ID) |
|---|
| 105 | scheduledTasks.Values[i] = task; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | public override void QueueTask(Task task) |
|---|
| 110 | { |
|---|
| 111 | lock (tasksLock) |
|---|
| 112 | { |
|---|
| 113 | //Set the task variable to indicate that the task is already |
|---|
| 114 | //waiting to be executed. |
|---|
| 115 | task.queued = true; |
|---|
| 116 | |
|---|
| 117 | //Queue the task to be run immediately. |
|---|
| 118 | scheduledTasks.Add(DateTime.Now, task); |
|---|
| 119 | schedulerInterrupt.Set(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | public override void QueueRestartTasks() |
|---|
| 124 | { |
|---|
| 125 | lock (tasksLock) |
|---|
| 126 | { |
|---|
| 127 | foreach (Task task in tasks.Values) |
|---|
| 128 | if (task.Schedule == Schedule.RunOnRestart) |
|---|
| 129 | QueueTask(task); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | public override void CancelTask(Task task) |
|---|
| 134 | { |
|---|
| 135 | lock (currentTask) |
|---|
| 136 | { |
|---|
| 137 | if (currentTask == task) |
|---|
| 138 | { |
|---|
| 139 | currentTask.cancelled = true; |
|---|
| 140 | return; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | lock (tasksLock) |
|---|
| 145 | for (int i = 0; i != scheduledTasks.Count; ++i) |
|---|
| 146 | if (scheduledTasks.Values[i] == task) |
|---|
| 147 | { |
|---|
| 148 | scheduledTasks.RemoveAt(i); |
|---|
| 149 | return; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | throw new ArgumentOutOfRangeException("The task to be cancelled must " + |
|---|
| 153 | "either be currently executing or queued."); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | public override Task GetTask(uint taskId) |
|---|
| 157 | { |
|---|
| 158 | lock (tasksLock) |
|---|
| 159 | { |
|---|
| 160 | if (!tasks.ContainsKey(taskId)) |
|---|
| 161 | return null; |
|---|
| 162 | return tasks[taskId]; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | public override List<Task> GetTasks() |
|---|
| 167 | { |
|---|
| 168 | lock (tasksLock) |
|---|
| 169 | { |
|---|
| 170 | Task[] result = new Task[tasks.Count]; |
|---|
| 171 | tasks.Values.CopyTo(result, 0); |
|---|
| 172 | return new List<Task>(result); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public override void SaveTaskList(Stream stream) |
|---|
| 177 | { |
|---|
| 178 | lock (tasksLock) |
|---|
| 179 | new BinaryFormatter().Serialize(stream, tasks); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | public override void LoadTaskList(Stream stream) |
|---|
| 183 | { |
|---|
| 184 | lock (tasksLock) |
|---|
| 185 | tasks = (Dictionary<uint, Task>)new BinaryFormatter().Deserialize(stream); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /// <summary> |
|---|
| 189 | /// The thread entry point for this object. This object operates on a queue |
|---|
| 190 | /// and hence the thread will sequentially execute tasks. |
|---|
| 191 | /// </summary> |
|---|
| 192 | private void Main() |
|---|
| 193 | { |
|---|
| 194 | //The waiting thread will utilize a polling loop to check for new |
|---|
| 195 | //scheduled tasks. This will be checked every 30 seconds. However, |
|---|
| 196 | //when the thread is waiting for a new task, it can be interrupted. |
|---|
| 197 | while (thread.ThreadState != ThreadState.AbortRequested) |
|---|
| 198 | { |
|---|
| 199 | //Check for a new task |
|---|
| 200 | Task task = null; |
|---|
| 201 | lock (tasksLock) |
|---|
| 202 | { |
|---|
| 203 | if (scheduledTasks.Count != 0 && |
|---|
| 204 | (scheduledTasks.Values[0].Schedule == Schedule.RunNow || |
|---|
| 205 | scheduledTasks.Keys[0] <= DateTime.Now)) |
|---|
| 206 | { |
|---|
| 207 | task = scheduledTasks.Values[0]; |
|---|
| 208 | scheduledTasks.RemoveAt(0); |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | if (task != null) |
|---|
| 213 | { |
|---|
| 214 | //Set the currently executing task. |
|---|
| 215 | currentTask = task; |
|---|
| 216 | |
|---|
| 217 | try |
|---|
| 218 | { |
|---|
| 219 | //Broadcast the task started event. |
|---|
| 220 | task.queued = false; |
|---|
| 221 | task.cancelled = false; |
|---|
| 222 | task.OnTaskStarted(new TaskEventArgs(task)); |
|---|
| 223 | |
|---|
| 224 | //Run the task |
|---|
| 225 | foreach (Task.ErasureTarget target in task.Targets) |
|---|
| 226 | try |
|---|
| 227 | { |
|---|
| 228 | if (target is Task.UnusedSpace) |
|---|
| 229 | EraseUnusedSpace(task, (Task.UnusedSpace)target); |
|---|
| 230 | else if (target is Task.FilesystemObject) |
|---|
| 231 | EraseFilesystemObject(task, (Task.FilesystemObject)target); |
|---|
| 232 | else |
|---|
| 233 | throw new ArgumentException("Unknown erasure target."); |
|---|
| 234 | } |
|---|
| 235 | catch (FatalException) |
|---|
| 236 | { |
|---|
| 237 | throw; |
|---|
| 238 | } |
|---|
| 239 | catch (Exception e) |
|---|
| 240 | { |
|---|
| 241 | task.LogEntry(new LogEntry(e.Message, LogLevel.ERROR)); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | catch (FatalException e) |
|---|
| 245 | { |
|---|
| 246 | task.LogEntry(new LogEntry(e.Message, LogLevel.FATAL)); |
|---|
| 247 | } |
|---|
| 248 | finally |
|---|
| 249 | { |
|---|
| 250 | //And the task finished event. |
|---|
| 251 | task.OnTaskFinished(new TaskEventArgs(task)); |
|---|
| 252 | |
|---|
| 253 | //If the task is a recurring task, reschedule it since we are done. |
|---|
| 254 | if (task.Schedule is RecurringSchedule) |
|---|
| 255 | ((RecurringSchedule)task.Schedule).Reschedule(DateTime.Now); |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | //Wait for half a minute to check for the next scheduled task. |
|---|
| 260 | schedulerInterrupt.WaitOne(30000, false); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /// <summary> |
|---|
| 265 | /// Executes a unused space erase. |
|---|
| 266 | /// </summary> |
|---|
| 267 | /// <param name="target">The target of the unused space erase.</param> |
|---|
| 268 | private void EraseUnusedSpace(Task task, Task.UnusedSpace target) |
|---|
| 269 | { |
|---|
| 270 | throw new NotImplementedException("Unused space erasures are not "+ |
|---|
| 271 | "currently implemented"); |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | /// <summary> |
|---|
| 275 | /// Erases a file or folder on the volume. |
|---|
| 276 | /// </summary> |
|---|
| 277 | /// <param name="target">The target of the erasure.</param> |
|---|
| 278 | private void EraseFilesystemObject(Task task, Task.FilesystemObject target) |
|---|
| 279 | { |
|---|
| 280 | //Retrieve the list of files to erase. |
|---|
| 281 | long totalSize = 0; |
|---|
| 282 | List<string> paths = target.GetPaths(out totalSize); |
|---|
| 283 | TaskProgressEventArgs eventArgs = new TaskProgressEventArgs(task, 0, 0); |
|---|
| 284 | |
|---|
| 285 | //Get the erasure method if the user specified he wants the default. |
|---|
| 286 | ErasureMethod method = target.Method; |
|---|
| 287 | if (method == ErasureMethodManager.Default) |
|---|
| 288 | method = ErasureMethodManager.GetInstance(Globals.Settings.DefaultFileErasureMethod); |
|---|
| 289 | |
|---|
| 290 | //Calculate the total amount of data required to finish the wipe. This |
|---|
| 291 | //value is just the total about of data to be erased multiplied by |
|---|
| 292 | //number of passes |
|---|
| 293 | totalSize *= method.Passes; |
|---|
| 294 | |
|---|
| 295 | //Record the start of the erasure pass so we can calculate speed of erasures |
|---|
| 296 | long totalLeft = totalSize; |
|---|
| 297 | long overallWriteSpeed = 0; |
|---|
| 298 | DateTime startTime = DateTime.Now; |
|---|
| 299 | |
|---|
| 300 | //Iterate over every path, and erase the path. |
|---|
| 301 | for (int i = 0; i < paths.Count; ++i) |
|---|
| 302 | { |
|---|
| 303 | //Update the task progress |
|---|
| 304 | eventArgs.overallProgress = (i * 100) / paths.Count; |
|---|
| 305 | eventArgs.currentTarget = target; |
|---|
| 306 | eventArgs.currentItemName = paths[i]; |
|---|
| 307 | eventArgs.currentItemProgress = 0; |
|---|
| 308 | eventArgs.totalPasses = method.Passes; |
|---|
| 309 | task.OnProgressChanged(eventArgs); |
|---|
| 310 | |
|---|
| 311 | //Make sure the file does not have any attributes which may |
|---|
| 312 | //affect the erasure process |
|---|
| 313 | FileInfo info = new FileInfo(paths[i]); |
|---|
| 314 | if ((info.Attributes & FileAttributes.Compressed) != 0 || |
|---|
| 315 | (info.Attributes & FileAttributes.Encrypted) != 0 || |
|---|
| 316 | (info.Attributes & FileAttributes.SparseFile) != 0 || |
|---|
| 317 | (info.Attributes & FileAttributes.ReparsePoint) != 0) |
|---|
| 318 | { |
|---|
| 319 | //Log the error |
|---|
| 320 | throw new ArgumentException("Compressed, encrypted, or sparse" + |
|---|
| 321 | "files cannot be erased with Eraser."); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | //Remove the read-only flag, if it is set. |
|---|
| 325 | if ((info.Attributes & FileAttributes.ReadOnly) != 0) |
|---|
| 326 | info.Attributes &= ~FileAttributes.ReadOnly; |
|---|
| 327 | |
|---|
| 328 | //Create the file stream, and call the erasure method |
|---|
| 329 | //to write to the stream. |
|---|
| 330 | using (FileStream strm = new FileStream(info.FullName, |
|---|
| 331 | FileMode.Open, FileAccess.Write, FileShare.None, |
|---|
| 332 | 8, FileOptions.WriteThrough)) |
|---|
| 333 | { |
|---|
| 334 | //Set the end of the stream after the wrap-round the cluster size |
|---|
| 335 | uint clusterSize = Drives.GetDriveClusterSize(info.Directory.Root.FullName); |
|---|
| 336 | long roundUpFileLength = strm.Length % clusterSize; |
|---|
| 337 | if (roundUpFileLength != 0) |
|---|
| 338 | strm.SetLength(strm.Length + (clusterSize - roundUpFileLength)); |
|---|
| 339 | |
|---|
| 340 | //Then erase the file. |
|---|
| 341 | method.Erase(strm, PRNGManager.GetInstance(Globals.Settings.ActivePRNG), |
|---|
| 342 | delegate(float currentProgress, int currentPass) |
|---|
| 343 | { |
|---|
| 344 | long amountWritten = (long)(currentProgress * (info.Length * method.Passes)); |
|---|
| 345 | if (overallWriteSpeed != 0) |
|---|
| 346 | eventArgs.timeLeft = (int)((totalLeft - amountWritten) / |
|---|
| 347 | overallWriteSpeed); |
|---|
| 348 | else if (amountWritten != 0 && (DateTime.Now - startTime).TotalSeconds != 0) |
|---|
| 349 | overallWriteSpeed = (long)(amountWritten / |
|---|
| 350 | (DateTime.Now - startTime).TotalSeconds); |
|---|
| 351 | |
|---|
| 352 | eventArgs.currentPass = currentPass; |
|---|
| 353 | eventArgs.currentItemProgress = (int) |
|---|
| 354 | ((float)currentProgress * 100.0); |
|---|
| 355 | eventArgs.overallProgress = (int) |
|---|
| 356 | (((i + currentProgress) / (float)paths.Count) * 100); |
|---|
| 357 | task.OnProgressChanged(eventArgs); |
|---|
| 358 | |
|---|
| 359 | lock (currentTask) |
|---|
| 360 | if (currentTask.cancelled) |
|---|
| 361 | throw new FatalException("The task was cancelled."); |
|---|
| 362 | } |
|---|
| 363 | ); |
|---|
| 364 | |
|---|
| 365 | //Update the speed-influencing statistics. |
|---|
| 366 | totalLeft -= info.Length * method.Passes; |
|---|
| 367 | overallWriteSpeed = (long)((totalSize - totalLeft) / |
|---|
| 368 | (DateTime.Now - startTime).TotalSeconds); |
|---|
| 369 | |
|---|
| 370 | //Set the length of the file to 0. |
|---|
| 371 | strm.Seek(0, SeekOrigin.Begin); |
|---|
| 372 | strm.SetLength(0); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | //Remove the file. |
|---|
| 376 | RemoveFile(info); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | //If the user requested a folder removal, do it. |
|---|
| 380 | if (target is Task.Folder) |
|---|
| 381 | { |
|---|
| 382 | Task.Folder fldr = (Task.Folder)target; |
|---|
| 383 | if (fldr.DeleteIfEmpty) |
|---|
| 384 | RemoveFolder(new DirectoryInfo(fldr.Path)); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | /// <summary> |
|---|
| 389 | /// Securely removes files. |
|---|
| 390 | /// </summary> |
|---|
| 391 | /// <param name="info">The FileInfo object representing the file.</param> |
|---|
| 392 | private void RemoveFile(FileInfo info) |
|---|
| 393 | { |
|---|
| 394 | //Set the date of the file to be invalid to prevent forensic |
|---|
| 395 | //detection |
|---|
| 396 | info.CreationTime = info.LastWriteTime = info.LastAccessTime = |
|---|
| 397 | new DateTime(1800, 1, 1, 0, 0, 0); |
|---|
| 398 | info.Attributes = FileAttributes.Normal; |
|---|
| 399 | info.Attributes = FileAttributes.NotContentIndexed; |
|---|
| 400 | |
|---|
| 401 | //Rename the file a few times to erase the record from the MFT. |
|---|
| 402 | for (int i = 0; i < FilenameErasePasses; ++i) |
|---|
| 403 | { |
|---|
| 404 | //Get a random file name |
|---|
| 405 | PRNG prng = PRNGManager.GetInstance(Globals.Settings.ActivePRNG); |
|---|
| 406 | byte[] newFileNameAry = new byte[info.Name.Length]; |
|---|
| 407 | prng.NextBytes(newFileNameAry); |
|---|
| 408 | |
|---|
| 409 | //Validate the name |
|---|
| 410 | const string validFileNameChars = "0123456789abcdefghijklmnopqrs" + |
|---|
| 411 | "tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 412 | for (int j = 0, k = newFileNameAry.Length; j < k; ++j) |
|---|
| 413 | newFileNameAry[j] = (byte)validFileNameChars[ |
|---|
| 414 | (int)newFileNameAry[j] % validFileNameChars.Length]; |
|---|
| 415 | |
|---|
| 416 | //Rename the file. |
|---|
| 417 | string newPath = info.DirectoryName + Path.DirectorySeparatorChar + |
|---|
| 418 | (new System.Text.UTF8Encoding()).GetString(newFileNameAry); |
|---|
| 419 | info.MoveTo(newPath); |
|---|
| 420 | } |
|---|
| 421 | |
|---|
| 422 | //Then delete the file. |
|---|
| 423 | info.Delete(); |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | private void RemoveFolder(DirectoryInfo info) |
|---|
| 427 | { |
|---|
| 428 | foreach (FileInfo file in info.GetFiles()) |
|---|
| 429 | RemoveFile(file); |
|---|
| 430 | foreach (DirectoryInfo dir in info.GetDirectories()) |
|---|
| 431 | RemoveFolder(dir); |
|---|
| 432 | |
|---|
| 433 | //Then clean up this folder. |
|---|
| 434 | for (int i = 0; i < FilenameErasePasses; ++i) |
|---|
| 435 | { |
|---|
| 436 | //Get a random file name |
|---|
| 437 | PRNG prng = PRNGManager.GetInstance(Globals.Settings.ActivePRNG); |
|---|
| 438 | byte[] newFileNameAry = new byte[info.Name.Length]; |
|---|
| 439 | prng.NextBytes(newFileNameAry); |
|---|
| 440 | |
|---|
| 441 | //Validate the name |
|---|
| 442 | const string validFileNameChars = "0123456789abcdefghijklmnopqrs" + |
|---|
| 443 | "tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 444 | for (int j = 0, k = newFileNameAry.Length; j < k; ++j) |
|---|
| 445 | newFileNameAry[j] = (byte)validFileNameChars[ |
|---|
| 446 | (int)newFileNameAry[j] % validFileNameChars.Length]; |
|---|
| 447 | |
|---|
| 448 | //Rename the folder. |
|---|
| 449 | string newPath = info.Parent.FullName + Path.DirectorySeparatorChar + |
|---|
| 450 | (new System.Text.UTF8Encoding()).GetString(newFileNameAry); |
|---|
| 451 | |
|---|
| 452 | //Try to rename the file. If it fails, it is probably due to another |
|---|
| 453 | //process locking the file. Defer, then rename again. |
|---|
| 454 | try |
|---|
| 455 | { |
|---|
| 456 | info.MoveTo(newPath); |
|---|
| 457 | } |
|---|
| 458 | catch (IOException) |
|---|
| 459 | { |
|---|
| 460 | Thread.Sleep(100); |
|---|
| 461 | --i; |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | //Remove the folder |
|---|
| 466 | info.Delete(); |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | /// <summary> |
|---|
| 470 | /// The thread object. |
|---|
| 471 | /// </summary> |
|---|
| 472 | private Thread thread; |
|---|
| 473 | |
|---|
| 474 | /// <summary> |
|---|
| 475 | /// The lock preventing concurrent access for the tasks list and the |
|---|
| 476 | /// tasks queue. |
|---|
| 477 | /// </summary> |
|---|
| 478 | private object tasksLock = new object(); |
|---|
| 479 | |
|---|
| 480 | /// <summary> |
|---|
| 481 | /// The list of tasks. Includes all immediate, reboot, and recurring tasks |
|---|
| 482 | /// </summary> |
|---|
| 483 | private Dictionary<uint, Task> tasks = new Dictionary<uint, Task>(); |
|---|
| 484 | |
|---|
| 485 | /// <summary> |
|---|
| 486 | /// The queue of tasks. This queue is executed when the first element's |
|---|
| 487 | /// timestamp (the key) has been past. This list assumes that all tasks |
|---|
| 488 | /// are sorted by timestamp, smallest one first. |
|---|
| 489 | /// </summary> |
|---|
| 490 | private SortedList<DateTime, Task> scheduledTasks = |
|---|
| 491 | new SortedList<DateTime, Task>(); |
|---|
| 492 | |
|---|
| 493 | /// <summary> |
|---|
| 494 | /// The currently executing task. |
|---|
| 495 | /// </summary> |
|---|
| 496 | Task currentTask; |
|---|
| 497 | |
|---|
| 498 | /// <summary> |
|---|
| 499 | /// The list of task IDs for recycling. |
|---|
| 500 | /// </summary> |
|---|
| 501 | private List<uint> unusedIds = new List<uint>(); |
|---|
| 502 | |
|---|
| 503 | /// <summary> |
|---|
| 504 | /// Lock preventing concurrent access for the IDs. |
|---|
| 505 | /// </summary> |
|---|
| 506 | private object unusedIdsLock = new object(); |
|---|
| 507 | |
|---|
| 508 | /// <summary> |
|---|
| 509 | /// Incrementing ID. This value is incremented by one every time an ID |
|---|
| 510 | /// is required by no unused IDs remain. |
|---|
| 511 | /// </summary> |
|---|
| 512 | private uint nextId = 0; |
|---|
| 513 | |
|---|
| 514 | /// <summary> |
|---|
| 515 | /// An automatically reset event allowing the addition of new tasks to |
|---|
| 516 | /// interrupt the thread's sleeping state waiting for the next recurring |
|---|
| 517 | /// task to be due. |
|---|
| 518 | /// </summary> |
|---|
| 519 | AutoResetEvent schedulerInterrupt = new AutoResetEvent(true); |
|---|
| 520 | } |
|---|
| 521 | } |
|---|