/// <summary> /// Copy a file locally and provide progress /// </summary> /// <param name="sourceFile">The file to be copied from</param> /// <param name="destFile">The file to be created / overwritten</param> /// <param name="onlyChanged"> /// Checks if the source file is newer or has a different length, if not file will not be copied, /// </param> /// <param name="processDisplay">A process display</param> public static async Task FileCopy([NotNull] string sourceFile, [NotNull] string destFile, bool onlyChanged, [NotNull] IProcessDisplay processDisplay) { if (onlyChanged) { var fiSource = new FileInfo(sourceFile); var fiDestInfo = new FileInfo(destFile); if (fiDestInfo.Exists && fiSource.LastWriteTimeUtc <= fiDestInfo.LastWriteTimeUtc && fiSource.Length == fiDestInfo.Length) { return; } } if (FileExists(sourceFile)) { FileDelete(destFile); } using (var fromStream = OpenRead(sourceFile)) using (var toStream = OpenWrite(destFile)) { var bytes = new byte[81920]; int bytesRead; long totalReads = 0; long oldMax = 0; if (processDisplay is IProcessDisplayTime processDisplayTime) { oldMax = processDisplayTime.Maximum; processDisplayTime.Maximum = fromStream.Length; } var intervalAction = new IntervalAction(); while ((bytesRead = await fromStream.ReadAsync(bytes, 0, bytes.Length, processDisplay.CancellationToken) .ConfigureAwait(false)) > 0) { processDisplay.CancellationToken.ThrowIfCancellationRequested(); totalReads += bytesRead; await toStream.WriteAsync(bytes, 0, bytesRead, processDisplay.CancellationToken).ConfigureAwait(false); intervalAction.Invoke(pos => processDisplay.SetProcess("Copy file", pos, false), totalReads); } processDisplay.SetMaximum(oldMax); } }
private void Work(string dataColumnName, bool ignoreNull) { if (string.IsNullOrEmpty(dataColumnName)) { return; } if (dataColumnName.Equals(m_LastDataColumnName, StringComparison.Ordinal) && m_LastIgnoreNull == ignoreNull) { return; } m_LastDataColumnName = dataColumnName; m_LastIgnoreNull = ignoreNull; this.SafeInvoke(() => { detailControl.Visible = false; detailControl.SuspendLayout(); }); Extensions.ProcessUIElements(); try { this.SafeBeginInvoke(() => Text = $"Unique Values Display - {dataColumnName} "); var dataColumnID = m_DataTable.Columns[dataColumnName]; var dictIDToRow = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase); var intervalAction = new IntervalAction(); using (var display = new FormProcessDisplay($"Processing {dataColumnName}", m_CancellationTokenSource.Token)) { display.Maximum = m_DataRow.Length; display.Show(this); for (var rowIdex = 0; rowIdex < m_DataRow.Length; rowIdex++) { if (display.CancellationToken.IsCancellationRequested) { return; } intervalAction.Invoke(delegate { display.SetProcess("Getting Unique values", rowIdex); }); var id = m_DataRow[rowIdex][dataColumnID.Ordinal].ToString().Trim(); if (ignoreNull && string.IsNullOrEmpty(id)) { continue; } if (!dictIDToRow.ContainsKey(id)) { dictIDToRow.Add(id, rowIdex); } } this.SafeInvoke(() => Text = $"Unique Values Display - {dataColumnName} - Rows {dictIDToRow.Count}/{m_DataRow.Length}"); m_DataTable.BeginLoadData(); m_DataTable.Clear(); display.Maximum = dictIDToRow.Count; var counter = 0; foreach (var rowIdex in dictIDToRow.Values) { if (display.CancellationToken.IsCancellationRequested) { return; } counter++; if (counter % 100 == 0) { intervalAction.Invoke(delegate { display.SetProcess("Importing Rows to Grid", counter); }); } m_DataTable.ImportRow(m_DataRow[rowIdex]); } m_DataTable.EndLoadData(); detailControl.CancellationToken = m_CancellationTokenSource.Token; display.Maximum = 0; display.SetProcess("Sorting"); detailControl.SafeInvoke(() => { try { foreach (DataGridViewColumn col in detailControl.DataGridView.Columns) { if (col.DataPropertyName == dataColumnName) { detailControl.DataGridView.Sort(col, ListSortDirection.Ascending); break; } } } catch (Exception ex) { Debug.WriteLine(ex.InnerExceptionMessages()); } }); } } catch (Exception ex) { this.ShowError(ex); } finally { this.SafeInvoke(() => { detailControl.Visible = true; detailControl.ResumeLayout(true); }); } }
/// <summary> /// Builds the tree data. /// </summary> private void BuildTreeData() { Contract.Requires(m_DataTable != null); var intervalAction = new IntervalAction(); using (var process = new FormProcessDisplay("Building Tree", m_CancellationTokenSource.Token)) { process.Show(this); process.Maximum = m_DataRow.GetLength(0); var dataColumnParent = m_DataTable.Columns[m_ComboBoxParentID.SelectedItem.ToString()]; var dataColumnID = m_DataTable.Columns[m_ComboBoxID.SelectedItem.ToString()]; var dataColumnDisplay1 = m_ComboBoxDisplay1.SelectedItem != null ? m_DataTable.Columns[m_ComboBoxDisplay1.SelectedItem.ToString()] : null; var dataColumnDisplay2 = m_ComboBoxDisplay2.SelectedItem != null ? m_DataTable.Columns[m_ComboBoxDisplay2.SelectedItem.ToString()] : null; // Using a dictionary here to speed up lookups var treeDataDictionary = new Dictionary <string, TreeData>(); var rootDataParentFound = new TreeData { ID = "{R}", Title = "Parent found / No Parent" }; treeDataDictionary.Add(rootDataParentFound.ID, rootDataParentFound); var counter = 0; foreach (var dataRow in m_DataRow) { process.CancellationToken.ThrowIfCancellationRequested(); counter++; intervalAction.Invoke(delegate { process.SetProcess($"Parent found {counter}/{process.Maximum} ", counter); }); var id = dataRow[dataColumnID.Ordinal].ToString(); if (string.IsNullOrEmpty(id)) { continue; } var treeData = new TreeData { ID = id, Title = dataColumnDisplay1 != null ? dataColumnDisplay2 != null ? dataRow[dataColumnDisplay1.Ordinal] + " - " + dataRow[dataColumnDisplay2.Ordinal] : dataRow[ dataColumnDisplay1.Ordinal].ToString() : id, ParentID = dataRow[dataColumnParent.Ordinal].ToString() }; if (dataColumnDisplay1 != null) { treeData.Tag = dataRow[dataColumnDisplay1.Ordinal].ToString(); } // Store the display if (!treeDataDictionary.ContainsKey(id)) { treeDataDictionary.Add(id, treeData); } } // Generate a list of missing parents var additionalRootNodes = new HashSet <string>(); foreach (var child in treeDataDictionary.Values) { if (!string.IsNullOrEmpty(child.ParentID) && !treeDataDictionary.ContainsKey(child.ParentID)) { additionalRootNodes.Add(child.ParentID); } } var rootDataParentNotFound = new TreeData { ID = "{M}", Title = "Parent not found" }; if (additionalRootNodes.Count > 0) { treeDataDictionary.Add(rootDataParentNotFound.ID, rootDataParentNotFound); counter = 0; process.Maximum = additionalRootNodes.Count; // Create new entries foreach (var parentID in additionalRootNodes) { process.CancellationToken.ThrowIfCancellationRequested(); counter++; intervalAction.Invoke(delegate { process.SetProcess($"Parent not found (Step 1) {counter}/{process.Maximum} ", counter); }); var childData = new TreeData { ParentID = rootDataParentNotFound.ID, ID = parentID, Title = $"{m_ComboBoxID.SelectedItem} - {parentID}" }; treeDataDictionary.Add(parentID, childData); } } process.Maximum = treeDataDictionary.Values.Count; counter = 0; foreach (var child in treeDataDictionary.Values) { process.CancellationToken.ThrowIfCancellationRequested(); counter++; intervalAction.Invoke(delegate { process.SetProcess($"Parent not found (Step 2) {counter}/{process.Maximum} ", counter); }); if (string.IsNullOrEmpty(child.ParentID) && child.ID != rootDataParentFound.ID && child.ID != rootDataParentNotFound.ID) { child.ParentID = rootDataParentFound.ID; } } process.Maximum = treeDataDictionary.Values.Count; counter = 0; // Fill m_Children for the new nodes foreach (var child in treeDataDictionary.Values) { process.CancellationToken.ThrowIfCancellationRequested(); counter++; intervalAction.Invoke(delegate { process.SetProcess($"Set children {counter}/{process.Maximum} ", counter); }); if (!string.IsNullOrEmpty(child.ParentID)) { treeDataDictionary[child.ParentID].Children.Add(child); } } m_TreeData = treeDataDictionary.Values; } }
/// <summary> /// Builds the tree data. /// </summary> private void BuildTreeData(string parentCol, string idCol, string display1, string display2, IProcessDisplay process) { var intervalAction = new IntervalAction(); var dataColumnParent = m_DataTable.Columns[parentCol]; if (dataColumnParent == null) { throw new ArgumentException($"Could not find column {parentCol}"); } var dataColumnID = m_DataTable.Columns[idCol]; if (dataColumnID == null) { throw new ArgumentException($"Could not find column {idCol}"); } var dataColumnDisplay1 = string.IsNullOrEmpty(display1) ? null : m_DataTable.Columns[display1]; var dataColumnDisplay2 = string.IsNullOrEmpty(display2) ? null : m_DataTable.Columns[display2]; // Using a dictionary here to speed up lookups var treeDataDictionary = new Dictionary <string, TreeData>(); var rootDataParentFound = new TreeData("{R}", "Parent found / No Parent"); treeDataDictionary.Add(rootDataParentFound.ID, rootDataParentFound); var max = 0L; if (process is IProcessDisplayTime processDisplayTime) { max = processDisplayTime.Maximum; } var counter = 0; foreach (var dataRow in m_DataRow) { process.CancellationToken.ThrowIfCancellationRequested(); intervalAction.Invoke( count => process.SetProcess($"Parent found {count}/{max} ", count, false), counter++); var id = dataRow[dataColumnID.Ordinal].ToString(); if (string.IsNullOrEmpty(id)) { continue; } var treeData = new TreeData(id, dataColumnDisplay1 != null ? dataColumnDisplay2 != null ? dataRow[dataColumnDisplay1.Ordinal] + " - " + dataRow[dataColumnDisplay2.Ordinal] : dataRow[dataColumnDisplay1.Ordinal].ToString() : id, dataRow[dataColumnParent.Ordinal].ToString()); if (dataColumnDisplay1 != null) { treeData.Tag = dataRow[dataColumnDisplay1.Ordinal].ToString(); } // Store the display if (!treeDataDictionary.ContainsKey(id)) { treeDataDictionary.Add(id, treeData); } } // Generate a list of missing parents var additionalRootNodes = new HashSet <string>(); foreach (var child in treeDataDictionary.Values) { if (!string.IsNullOrEmpty(child.ParentID) && !treeDataDictionary.ContainsKey(child.ParentID)) { additionalRootNodes.Add(child.ParentID); } } var rootDataParentNotFound = new TreeData("{M}", "Parent not found"); if (additionalRootNodes.Count > 0) { treeDataDictionary.Add(rootDataParentNotFound.ID, rootDataParentNotFound); counter = 0; max = additionalRootNodes.Count; process.SetMaximum(max); // Create new entries foreach (var parentID in additionalRootNodes) { process.CancellationToken.ThrowIfCancellationRequested(); intervalAction.Invoke( count => process.SetProcess($"Parent not found (Step 1) {count}/{max} ", count, false), counter++); var childData = new TreeData(parentID, $"{parentID}", rootDataParentNotFound.ID); treeDataDictionary.Add(parentID, childData); } } max = treeDataDictionary.Values.Count; process.SetMaximum(max); counter = 0; foreach (var child in treeDataDictionary.Values) { process.CancellationToken.ThrowIfCancellationRequested(); intervalAction.Invoke( count => process.SetProcess($"Parent not found (Step 2) {count}/{max} ", count, false), counter++); if (string.IsNullOrEmpty(child.ParentID) && child.ID != rootDataParentFound.ID && child.ID != rootDataParentNotFound.ID) { child.ParentID = rootDataParentFound.ID; } } max = treeDataDictionary.Values.Count; process.SetMaximum(max); counter = 0; // Fill m_Children for the new nodes foreach (var child in treeDataDictionary.Values) { process.CancellationToken.ThrowIfCancellationRequested(); intervalAction.Invoke( count => process.SetProcess($"Set children {count}/{max} ", count, false), counter++); if (!string.IsNullOrEmpty(child.ParentID)) { treeDataDictionary[child.ParentID].Children.Add(child); } } m_TreeData = treeDataDictionary.Values; }
private void Work(string dataColumnName, bool ignoreNull) { if (string.IsNullOrEmpty(dataColumnName)) { return; } if (dataColumnName.Equals(m_LastDataColumnName, StringComparison.OrdinalIgnoreCase) && m_LastIgnoreNull == ignoreNull) { return; } m_LastDataColumnName = dataColumnName; m_LastIgnoreNull = ignoreNull; this.SafeInvoke( () => { detailControl.Visible = false; detailControl.SuspendLayout(); }); try { var dupliacteList = new List <int>(); var dictIDToRow = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase); var dictFirstIDStored = new HashSet <int>(); var dataColumnID = m_DataTable.Columns[dataColumnName]; this.SafeInvoke(() => Text = $@"Duplicate Display - {dataColumnName}"); var intervalAction = new IntervalAction(); using (var display = new FormProcessDisplay( $"Processing {dataColumnName}", false, m_CancellationTokenSource.Token)) { display.Maximum = m_DataRow.Length; display.Show(this); for (var rowIndex = 0; rowIndex < m_DataRow.Length; rowIndex++) { if (display.CancellationToken.IsCancellationRequested) { return; } // ReSharper disable once AccessToDisposedClosure intervalAction.Invoke(row => display.SetProcess("Getting duplicate values", row, false), rowIndex); var id = m_DataRow[rowIndex][dataColumnID.Ordinal].ToString().Trim(); // if (id != null) id = id.Trim(); if (ignoreNull && string.IsNullOrEmpty(id)) { continue; } if (dictIDToRow.TryGetValue(id, out var duplicateRowIndex)) { if (!dictFirstIDStored.Contains(duplicateRowIndex)) { dupliacteList.Add(duplicateRowIndex); dictFirstIDStored.Add(duplicateRowIndex); } dupliacteList.Add(rowIndex); } else { dictIDToRow.Add(id, rowIndex); } } dictFirstIDStored.Clear(); dictIDToRow.Clear(); this.SafeInvoke( () => Text = $@"Duplicate Display - {dataColumnName} - Rows {dupliacteList.Count} / {m_DataRow.Length}"); m_DataTable.BeginLoadData(); m_DataTable.Clear(); var counter = 0; display.Maximum = dupliacteList.Count; foreach (var rowIndex in dupliacteList) { if (display.CancellationToken.IsCancellationRequested) { return; } intervalAction.Invoke(row => display.SetProcess("Importing Rows to Grid", row, false), counter++); m_DataTable.ImportRow(m_DataRow[rowIndex]); } m_DataTable.EndLoadData(); display.Maximum = 0; display.SetProcess("Sorting"); detailControl.SafeInvoke( () => { try { foreach (DataGridViewColumn col in detailControl.FilteredDataGridView.Columns) { if (col.DataPropertyName == dataColumnName) { detailControl.FilteredDataGridView.Sort(col, ListSortDirection.Ascending); break; } } } catch (Exception ex) { Logger.Warning(ex, "Sorting duplicate list {exception}", ex.SourceExceptionMessage()); } }); } } finally { this.SafeInvoke( () => { detailControl.Visible = true; detailControl.ResumeLayout(true); }); } }