private static void uxBGProcess_DoWork(object sender, DoWorkEventArgs e) { try { Dictionary<String, object> objArgument = (Dictionary<String, object>)e.Argument; List<String> arguements = (List<String>)objArgument["DefragArguements"]; StringBuilder output = new StringBuilder(); output.AppendLine(); foreach (String argument in arguements) { string driveName = argument.Substring(0, 1).ToUpper(); output.AppendLine("Drive Name :" + driveName.ToUpper()); output.AppendLine("-----------------------------------"); commandObj = new CommandObj(); commandObj.ProcessName = PROCESS_NAME; String result = commandObj.Run(DEFRAGMENTATION, argument, DEFRAGMENTATION + ".out", ""); output.AppendLine(result); output.AppendLine(); output.AppendLine(); if (commandObj.IsCancelled) return; //LogMessage.WriteLogInfo("DefragmentationService.cs : uxBGProcess_DoWork() : Analyze/Defragmentation process was completed for " + argument.Substring(0, 1).ToUpper() + "' Drive"); } e.Result = output.ToString(); //LogMessage.WriteLogInfo("DefragmentationService.cs : uxBGProcess_DoWork() : Analyze/Defragmentation process was completed for all selected drives."); } catch (Exception) { e.Cancel = true; //LogMessage.WriteErrorInfo("DefragmentationService.cs : uxBGProcess_DoWork() :: " + ex.Message); } }
public static void StartDefragmentation() { commandObj = new CommandObj(); try { List<String> arguments = new List<string>(); String argument = String.Empty; //Now.. Lets build our command with its arguments... /* * -a : Analyse * -f : Force defragment even if free space is lower than required * -v : Get the verbose output (Detailed Analysis Report) */ List<string> selectedDrives = GetSystemDrives(); for (int index = 0; index < selectedDrives.Count; index++) { argument = selectedDrives[index].ToString().Replace("\\", "").ToLower(); argument = argument + " -a "; //argument = argument + " -v "; arguments.Add(argument); } Dictionary<String, object> objArgument = new Dictionary<string, object>(); objArgument.Add("DefragArguements", arguments); //Let's defragment in the background... BackgroundWorker uxBGProcess1 = new BackgroundWorker(); uxBGProcess1.WorkerSupportsCancellation = true; uxBGProcess1.DoWork += new DoWorkEventHandler(uxBGProcess_DoWork); uxBGProcess1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(uxBGProcess_RunWorkerCompleted); //Trigger the worker thread... uxBGProcess1.RunWorkerAsync((object)objArgument); } catch (Exception ex) { //LogMessage.WriteErrorInfo("DefragmentationService.cs : uxBGProcess_DoWork() :: " + ex.Message); throw ex; } }