/// <summary> /// Called when processing has been completed or aborted. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnComplete(object sender, ShowProgressArgs e) { if (IsDisposed) { return; } if (InvokeRequired == false) { statusBar.Text = e.StatusMessage; if (e.Failed) { textBoxResult.Text = ""; MessageBox.Show(e.StatusMessage, "Operation failed.", MessageBoxButtons.OK, MessageBoxIcon.Error); } textBoxResult.Text = e.FileList; MakeStateTransition(ComputeState.Idle); } else { Computer.ProgressEvent completeHandler = OnComplete; Invoke(completeHandler, sender, e); } }
/// <summary> /// Called periodically while computing. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnProgress(object sender, ShowProgressArgs e) { try { if (InvokeRequired == false) { if (e.StatusMessage.Length > 0) { statusBarText.Text = e.StatusMessage; } else { statusBarText.Text = $"Selected {NumberFormatter.To1024BaseString(e.BytesUsed, 3, "bytes")}, unused space is {NumberFormatter.To1024BaseString(e.BytesTotal - e.BytesUsed, 3, "bytes")} ({NumberFormatter.ToPercentage(e.BytesUsed, e.BytesTotal):f2}%)."; } textBoxResult.Text = e.FileList; } else { Computer.ProgressEvent showProgress = OnProgress; Invoke(showProgress, sender, e); } } catch (ObjectDisposedException) { // User decided to quit e.Cancel = true; } }
private void UpdateProgress(string statusMessage, bool failed) { if (BetterMatch != null) { ShowProgressArgs e = new ShowProgressArgs(_bestBytes, _maxBytes); if (_bestSelelection != null) { e.FileList = GetBestFileText(); } e.StatusMessage = statusMessage; e.Failed = failed; BetterMatch(this, e); if (e.Cancel) { Stop(); } } }
public void FindOptimalCombination() { _cancel = false; ShowProgressArgs progress = new ShowProgressArgs(0, _maxBytes); try { BuildFileList(_sourcePath, _nestingLevel); if (_cancel) { return; } _bestBytes = 0; _bestSelelection = new bool[_sourceFiles.Length]; _currentSelelection = new bool[_sourceFiles.Length]; for (int y = 0; y < _currentSelelection.Length; y++) { _currentSelelection[y] = false; } // Sorting descending makes it easier to test the largest files first. Array.Sort(_sourceFiles); Array.Reverse(_sourceFiles); if (_sourceFiles.Length <= 0) { throw new ApplicationException( $"No file was found on source path '{_sourcePath}'."); } if (_sourceFiles[_sourceFiles.GetUpperBound(0)].Size > _maxBytes) { throw new ApplicationException("All files are too big for target."); } if (_maxBytes > _sourceBytes) { // Not enough source files, select all and quit. for (int i = 0; i < _bestSelelection.Length; i++) { _bestSelelection[i] = true; } _bestBytes = _sourceBytes; UpdateProgress(string.Empty, false); } else { FindOptimalCombination(0, 0); } } catch (Exception caught) { progress.Failed = true; progress.StatusMessage = caught.Message; } finally { if (ComputeComplete != null) { progress.BytesUsed = _bestBytes; if (!progress.Failed) { progress.FileList = GetBestFileText(); } ComputeComplete(this, progress); } } }