Changeset 804


Ignore:
Timestamp:
12/12/2008 6:48:57 AM (3 years ago)
Author:
lowjoel
Message:

Break up the ProgressManager? class into the same base class and a TaskProgressManager? class which handles a task as well. This partially implements #90

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Manager/DirectExecutor.cs

    r803 r804  
    310310 
    311311                        //Run the task 
    312                         ProgressManager progress = new ProgressManager(currentTask); 
     312                        TaskProgressManager progress = new TaskProgressManager(currentTask); 
    313313                        foreach (Task.ErasureTarget target in task.Targets) 
    314314                            try 
     
    369369 
    370370        /// <summary> 
    371         /// Provides a common interface to track the progress made by the Erase functions. 
     371        /// Manages the progress for any operation. 
    372372        /// </summary> 
    373373        private class ProgressManager 
    374374        { 
    375375            /// <summary> 
    376             /// Constructor. 
     376            /// Starts measuring the speed of the task. 
    377377            /// </summary> 
    378             public ProgressManager(Task task) 
     378            public void Start() 
    379379            { 
    380380                startTime = DateTime.Now; 
    381                 Event = new TaskProgressEventArgs(task); 
    382  
    383                 foreach (Task.ErasureTarget target in task.Targets) 
    384                     totalData += target.TotalData; 
    385381            } 
    386382 
    387383            /// <summary> 
    388             /// Computes the speed of the erase, in bytes per second, based on the 
    389             /// information collected in the previous 10 seconds. 
     384            /// Tracks the amount of the operation completed. 
     385            /// </summary> 
     386            public long Completed 
     387            { 
     388                get 
     389                { 
     390                    return completed; 
     391                } 
     392                set 
     393                { 
     394                    completed = value; 
     395                } 
     396            } 
     397 
     398            /// <summary> 
     399            /// The amount to reach before the operation completes. 
     400            /// </summary> 
     401            public long Total 
     402            { 
     403                get 
     404                { 
     405                    return total; 
     406                } 
     407                set 
     408                { 
     409                    total = value; 
     410                } 
     411            } 
     412 
     413            /// <summary> 
     414            /// Gets the percentage of the operation completed. 
     415            /// </summary> 
     416            public float Progress 
     417            { 
     418                get 
     419                { 
     420                    return (float)((double)Completed / Total); 
     421                } 
     422            } 
     423 
     424            /// <summary> 
     425            /// Computes the speed of the erase, in units of completion per second, 
     426            /// based on the information collected in the previous 5 seconds. 
    390427            /// </summary> 
    391428            public int Speed 
     
    400437 
    401438                    lastSpeedCalc = DateTime.Now; 
    402                     lastSpeed = (int)(DataWritten / (DateTime.Now - startTime).TotalSeconds); 
     439                    lastSpeed = (int)(Completed / (DateTime.Now - startTime).TotalSeconds); 
    403440                    return lastSpeed; 
    404441                } 
     
    415452                    if (Speed == 0) 
    416453                        return new TimeSpan(0, 0, -1); 
    417                     return new TimeSpan(0, 0, (int)((totalData - DataWritten) / Speed)); 
     454                    return new TimeSpan(0, 0, (int)((Total - Completed) / Speed)); 
    418455                } 
    419456            } 
    420457 
    421458            /// <summary> 
    422             /// Tracks the amount of data written, to determine the speed of the erase. 
     459            /// The starting time of the operation, used to determine average speed. 
    423460            /// </summary> 
    424             public long DataWritten 
    425             { 
    426                 get 
    427                 { 
    428                     return dataWritten; 
    429                 } 
    430                 set 
    431                 { 
    432                     dataWritten = value; 
    433                 } 
     461            private DateTime startTime; 
     462 
     463            /// <summary> 
     464            /// The last time a speed calculation was computed so that speed is not 
     465            /// computed too often. 
     466            /// </summary> 
     467            private DateTime lastSpeedCalc; 
     468 
     469            /// <summary> 
     470            /// The last calculated speed of the operation. 
     471            /// </summary> 
     472            private int lastSpeed; 
     473 
     474            /// <summary> 
     475            /// The amount of the operation completed. 
     476            /// </summary> 
     477            private long completed; 
     478 
     479            /// <summary> 
     480            /// The amount to reach before the operation is completed. 
     481            /// </summary> 
     482            private long total; 
     483        } 
     484 
     485        /// <summary> 
     486        /// Provides a common interface to track the progress made by the Erase functions. 
     487        /// </summary> 
     488        private class TaskProgressManager : ProgressManager 
     489        { 
     490            /// <summary> 
     491            /// Constructor. 
     492            /// </summary> 
     493            public TaskProgressManager(Task task) 
     494            { 
     495                foreach (Task.ErasureTarget target in task.Targets) 
     496                    Total += target.TotalData; 
     497 
     498                Event = new TaskProgressEventArgs(task); 
     499                Start(); 
    434500            } 
    435501 
     
    450516            } 
    451517 
    452             private long totalData; 
    453             private DateTime startTime; 
    454             private DateTime lastSpeedCalc; 
    455             private int lastSpeed; 
    456             private long dataWritten; 
    457518            private TaskProgressEventArgs evt; 
    458519        } 
     
    464525        /// <param name="target">The target of the unused space erase.</param> 
    465526        /// <param name="progress">The progress manager object managing the progress of the task</param> 
    466         private void EraseUnusedSpace(Task task, Task.UnusedSpace target, ProgressManager progress) 
     527        private void EraseUnusedSpace(Task task, Task.UnusedSpace target, TaskProgressManager progress) 
    467528        { 
    468529            //Check for sufficient privileges to run the unused space erasure. 
    469530            if (!Permissions.IsAdministrator()) 
    470531            { 
    471                  
    472532                if (Environment.OSVersion.Platform == PlatformID.Win32NT && 
    473533                    Environment.OSVersion.Version >= new Version(6, 0)) 
     
    498558                progress.Event.timeLeft = progress.TimeLeft; 
    499559                task.OnProgressChanged(progress.Event); 
    500                  
     560 
     561                ProgressManager tipProgress = new ProgressManager(); 
     562                tipProgress.Start(); 
    501563                EraseClusterTips(task, target, method, 
    502564                    delegate(int currentFile, string currentFilePath, int totalFiles) 
    503565                    { 
     566                        tipProgress.Total = totalFiles; 
     567                        tipProgress.Completed = currentFile; 
     568 
    504569                        progress.Event.currentItemName = S._("(Tips) {0}", currentFilePath); 
    505                         progress.Event.currentItemProgress = (float)currentFile / totalFiles; 
     570                        progress.Event.currentItemProgress = tipProgress.Progress; 
    506571                        progress.Event.CurrentTargetProgress = progress.Event.CurrentItemProgress / 10; 
     572                        progress.Event.timeLeft = tipProgress.TimeLeft; 
    507573                        task.OnProgressChanged(progress.Event); 
    508574                    } 
     
    573639                            delegate(long lastWritten, int currentPass) 
    574640                            { 
    575                                 progress.DataWritten += lastWritten; 
     641                                progress.Completed += lastWritten; 
    576642                                progress.Event.currentItemPass = currentPass; 
    577                                 progress.Event.currentItemProgress = (float)progress.DataWritten / totalSize; 
     643                                progress.Event.currentItemProgress = progress.Progress; 
    578644                                if (target.EraseClusterTips) 
    579645                                    progress.Event.CurrentTargetProgress = (float) 
    580                                         (0.1 + progress.Event.currentItemProgress * 0.8); 
     646                                        (0.1f + progress.Event.currentItemProgress * 0.8f); 
    581647                                else 
    582648                                    progress.Event.CurrentTargetProgress = (float) 
    583                                         (progress.Event.currentItemProgress * 0.9); 
     649                                        (progress.Event.currentItemProgress * 0.9f); 
    584650                                progress.Event.timeLeft = progress.TimeLeft; 
    585651                                task.OnProgressChanged(progress.Event); 
     
    608674            //Then clean the old file system entries 
    609675            progress.Event.currentItemName = S._("Old file system entries"); 
    610             DateTime fsEntriesEraseStart = DateTime.Now; 
     676            ProgressManager fsEntriesProgress = new ProgressManager(); 
     677            fsEntriesProgress.Start(); 
    611678            EraseOldFilesystemEntries(info.Parent, 
    612679                delegate(int currentFile, int totalFiles) 
    613680                { 
    614                     progress.Event.currentItemProgress = (float)currentFile / totalFiles; 
     681                    //Compute the progress 
     682                    fsEntriesProgress.Total = totalFiles; 
     683                    fsEntriesProgress.Completed = currentFile; 
     684 
     685                    //Set the event parameters, then broadcast the progress event. 
     686                    progress.Event.timeLeft = fsEntriesProgress.TimeLeft; 
     687                    progress.Event.currentItemProgress = fsEntriesProgress.Progress; 
    615688                    progress.Event.CurrentTargetProgress = (float)( 
    616689                        0.9 + progress.Event.CurrentItemProgress / 10); 
    617  
    618                     //Calculate how much time will be needed to complete the erase 
    619                     TimeSpan elapsedTime = DateTime.Now - fsEntriesEraseStart; 
    620                     if (elapsedTime > TimeSpan.Zero) 
    621                     { 
    622                         progress.Event.timeLeft = new TimeSpan(0, 0, (int) 
    623                             ((float)(totalFiles - currentFile) / currentFile * 
    624                                 elapsedTime.TotalSeconds)); 
    625                     } 
    626  
    627690                    task.OnProgressChanged(progress.Event); 
    628691                } 
     
    663726                                "erased because it is a hard link or a symbolic link.", 
    664727                                file.FullName), LogLevel.INFORMATION)); 
    665                         else if ((file.Attributes & FileAttributes.Compressed) || 
    666                             (file.Attributes & FileAttributes.Encrypted) || 
    667                             (file.Attributes & FileAttributes.SparseFile)) 
     728                        else if ((file.Attributes & FileAttributes.Compressed) != 0 || 
     729                            (file.Attributes & FileAttributes.Encrypted) != 0 || 
     730                            (file.Attributes & FileAttributes.SparseFile) != 0) 
    668731                        { 
    669732                            task.Log.Add(new LogEntry(S._("{0} did not have its cluster tips " + 
     
    915978        /// <param name="progress">The progress manager for the current task.</param> 
    916979        private void EraseFilesystemObject(Task task, Task.FilesystemObject target, 
    917             ProgressManager progress) 
     980            TaskProgressManager progress) 
    918981        { 
    919982            //Retrieve the list of files to erase. 
     
    9771040                                { 
    9781041                                    dataTotal -= lastWritten; 
    979                                     progress.DataWritten += lastWritten; 
     1042                                    progress.Completed += lastWritten; 
    9801043                                    progress.Event.currentItemPass = currentPass; 
    9811044                                    progress.Event.currentItemProgress = (float) 
Note: See TracChangeset for help on using the changeset viewer.