private void BtnOk_Click(object sender, EventArgs e) { var dialogResult = MessageBox.Show(Resources.MessageBox_DeleteAllFilesReally, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (dialogResult == DialogResult.Yes) { int removedCount = 0; foreach (var listViewItem in lvCleanupHandlers.Items.OfType <ListViewItem>()) { if (listViewItem.Tag is CleanupHandler cHandler) { if (!lblShoreMoreSettings.Visible) { cHandler.StateFlag = listViewItem.Checked; CleanupApi.UpdateHandlerStateFlag(cHandler); } // Get rid of handlers that we won't need early on if (!listViewItem.Checked) { try { cHandler.Instance.Deactivate(out uint dummy); } catch { } Marshal.FinalReleaseComObject(cHandler.Instance); Preferences.CleanupHandlers.Remove(cHandler); removedCount++; } } } CleanupApi.DeactivateHandlers(Preferences.CleanupHandlers); DialogResult = DialogResult.OK; } }
// Events protected override void OnLoad(EventArgs e) { base.OnLoad(e); CleanupApi.ReinstateHandlers(Preferences.CleanupHandlers, Preferences.SelectedDrive.Letter); InitializeListViewItems(); CalculateSelectedSavings(); }
private void Run() { lblCurrentHandler.Text = Resources.Label_Preparing.Format(Preferences.CleanupHandlers[0].DisplayName); _thread = new Thread(new ThreadStart(() => { CleanupApi.ReinstateHandlers(Preferences.CleanupHandlers, Preferences.SelectedDrive.Letter); // Set up a callback for progress reporting _callBacks = new EmptyVolumeCacheCallBacks(); _callBacks.PurgeProgressChanged += CallBacks_PurgeProgressChanged; _totalBytesDeleted = 0; _processingHandlers = true; for (int i = 0; i < Preferences.CleanupHandlers.Count; i++) { _currentHandler = Preferences.CleanupHandlers[i]; if (_currentHandler.PreProcHint != null) { RunProcHint(_currentHandler.PreProcHint); } _lastHandlerPercent = 0; if (i > 0 && lblCurrentHandler.IsHandleCreated) { Invoke((MethodInvoker) delegate { lblCurrentHandler.Text = Resources.Label_Preparing.Format(_currentHandler.DisplayName); }); } int spaceResult = _currentHandler.Instance.GetSpaceUsed(out long newSpaceUsed, _callBacks); if (spaceResult >= 0) { Preferences.CurrentSelectionSavings = Preferences.CurrentSelectionSavings - _currentHandler.BytesUsed + newSpaceUsed; _currentHandler.BytesUsed = newSpaceUsed; ReportLastHandlerPercent(); int purgeResult = _currentHandler.Instance.Purge(_currentHandler.BytesUsed, _callBacks); if (purgeResult == -2147467260) // -2147467260 = 0x80004004 = E_ABORT { break; } if (purgeResult < 0 && purgeResult != -2147287022) // -2147287022 == 0x80030012 == STG_E_NOMOREFILES { MessageBox.Show("Purging " + _currentHandler.DisplayName + " failed with error 0x" + purgeResult.ToString("x8"), Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } if (_currentHandler.PostProcHint != null) { RunProcHint(_currentHandler.PostProcHint); } } try { _currentHandler.Instance.Deactivate(out uint dummy); } catch { } Marshal.FinalReleaseComObject(_currentHandler.Instance); if (spaceResult < 0) { break; } } _processingHandlers = false; CleanupApi.DeactivateHandlers(Preferences.CleanupHandlers); // A (stupid?) way to close the form once we are done cleaning Invoke((MethodInvoker) delegate { Close(); }); })); _thread.SetApartmentState(ApartmentState.STA); _thread.Start(); }
// Methods private void Run() { _thread = new Thread(new ThreadStart(() => { Preferences.CleanupHandlers = new List <CleanupHandler>(); using (var registryKey = Registry.LocalMachine.OpenSubKey(CleanupApi.VolumeCacheStoreKey, false)) { var subKeyNames = registryKey.GetSubKeyNames(); // Adjust progress bar maximum to discovered handler count Invoke((MethodInvoker) delegate { pgrScanning.Maximum = subKeyNames.Length; }); // Set up a dummy callback because COM stuff goes haywire for particular handlers if we supply none var callBacks = new EmptyVolumeCacheCallBacks(); for (var i = 0; i < subKeyNames.Length; i++) { var cleanupHandler = CleanupApi.InitializeHandler(subKeyNames[i], Preferences.SelectedDrive.Letter); if (cleanupHandler != null) { if (lblCurrentHandler.IsHandleCreated) { Invoke((MethodInvoker) delegate { lblCurrentHandler.Text = cleanupHandler.DisplayName; pgrScanning.Value = i + 1; }); } var spaceResult = cleanupHandler.Instance.GetSpaceUsed(out long spaceUsed, callBacks); if (spaceResult < 0 || (spaceUsed == 0 && ((cleanupHandler.Flags & HandlerFlags.DontShowIfZero) == HandlerFlags.DontShowIfZero || (cleanupHandler.DataDrivenFlags & DDCFlags.DontShowIfZero) == DDCFlags.DontShowIfZero))) { try { cleanupHandler.Instance.Deactivate(out uint dummy); } catch { } Marshal.FinalReleaseComObject(cleanupHandler.Instance); } else { cleanupHandler.BytesUsed = spaceUsed; Preferences.CleanupHandlers.Add(cleanupHandler); } } } } CleanupApi.DeactivateHandlers(Preferences.CleanupHandlers); // Sort handlers by priority, making sure they'll run in correct order Preferences.CleanupHandlers.Sort((x, y) => y.Priority.CompareTo(x.Priority)); // A (stupid?) way to close the form once we are done cleaning Invoke((MethodInvoker) delegate { Close(); }); })); _thread.SetApartmentState(ApartmentState.STA); _thread.Start(); }