public async Task StartAsync([NotNull] IFileSetting fileSetting, bool includeError, TimeSpan durationInitial, [NotNull] IProcessDisplay processDisplay, [CanBeNull] EventHandler <WarningEventArgs> addWarning) { m_FileReader = FunctionalDI.GetFileReader(fileSetting, TimeZoneInfo.Local.Id, processDisplay); if (m_FileReader == null) { throw new FileReaderException($"Could not get reader for {fileSetting}"); } RowErrorCollection warningList = null; if (addWarning != null) { warningList = new RowErrorCollection(m_FileReader); m_FileReader.Warning += addWarning; m_FileReader.Warning -= warningList.Add; } Logger.Information("Reading data for display"); await m_FileReader.OpenAsync(processDisplay.CancellationToken).ConfigureAwait(false); if (addWarning != null) { warningList.HandleIgnoredColumns(m_FileReader); warningList.PassWarning += addWarning; } m_DataReaderWrapper = new DataReaderWrapper(m_FileReader, fileSetting.RecordLimit, includeError, fileSetting.DisplayStartLineNo, fileSetting.DisplayRecordNo, fileSetting.DisplayEndLineNo); m_ActionBegin?.Invoke(); await GetBatchByTimeSpan(durationInitial, includeError, processDisplay, m_SetDataTable).ConfigureAwait(false); m_SetLoadNextBatchAsync?.Invoke(process => GetBatchByTimeSpan(TimeSpan.MaxValue, includeError, process, (dt) => m_GetDataTable().Merge(dt))); m_ActionFinished?.Invoke(m_DataReaderWrapper); }
/// <summary> /// Writes the file ans displays performance information /// </summary> /// <param name="fileSetting">The file setting.</param> /// <param name="showSummary"> /// Flag indicating that a message Box should be displayed. If <c>true</c> a message box will be /// shown /// </param> /// <param name="cancellationToken">A CancellationToken</param> /// <param name="fileSettingSourcesCurrent">The file setting sources current.</param> /// <param name="settingLaterThanSources">The setting later than sources.</param> /// <param name="ask">if set to <c>true</c> [ask].</param> /// <returns> /// Number of record written to file /// </returns> public static long WriteFileWithInfo(this IFileSetting fileSetting, bool showSummary, FileSettingChecker fileSettingSourcesCurrent, bool ask, bool onlyOlder, CancellationToken cancellationToken) { if (fileSetting == null) { return(0); } long written = 0; var fi = FileSystemUtils.FileInfo(fileSetting.FullPath); var dir = FileSystemUtils.GetDirectoryName(fi.FullName); if (!FileSystemUtils.DirectoryExists(dir)) { if (_MessageBox.Show(null, $"The directory {dir.RemovePrefix()} does not exist, should it be created?", "Directory", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { FileSystemUtils.CreateDirectory(dir); } else { return(0); } } var fileInfo = new FileInfo(fileSetting.FullPath); if (fileInfo.Exists) { fileSetting.FileLastWriteTimeUtc = fi.LastWriteTimeUtc; } var stringBuilder = new System.Text.StringBuilder(); using (var processDisplay = fileSetting.GetProcessDisplay(null, true, cancellationToken)) { fileSettingSourcesCurrent?.Invoke(fileSetting, processDisplay); if (onlyOlder && fileSetting.SettingLaterThanSources(processDisplay.CancellationToken)) { return(0); } fileSetting.FullPath.DeleteFileQuestion(ask); var errors = new RowErrorCollection(50); var writer = fileSetting.GetFileWriter(processDisplay.CancellationToken); writer.ProcessDisplay = processDisplay; writer.Warning += errors.Add; written = writer.Write(); var hasIssues = !string.IsNullOrEmpty(writer.ErrorMessage) || (errors.CountRows > 0); if (showSummary || hasIssues) { fi = FileSystemUtils.FileInfo(fileSetting.FullPath); // if all source settings are file settings, get the latest file time and set this fileTime var latest = DateTime.MinValue; fileSetting.GetSourceFileSettings(delegate(IFileSetting setting) { if (!(setting is IFileSettingRemoteDownload)) { if (latest < setting.FileLastWriteTimeUtc) { latest = setting.FileLastWriteTimeUtc; } } else { var fiSrc = FileSystemUtils.FileInfo(setting.FullPath); if (fiSrc.Exists && latest < fiSrc.LastWriteTimeUtc) { latest = fiSrc.LastWriteTimeUtc; } } return(false); }, cancellationToken); stringBuilder.Append($"Finished writing file\r\rRecords: {written:N0}\rFile size: {fi.Length / 1048576.0:N} MB"); if (latest < DateTime.MaxValue && latest > DateTime.MinValue) { stringBuilder.Append($"\rTime adjusted to latest source file: {latest.ToLocalTime():D}"); fi.LastWriteTimeUtc = latest; fileSetting.FileLastWriteTimeUtc = latest; } if (hasIssues) { stringBuilder.Append("\rIssues:\r"); } if (!string.IsNullOrEmpty(writer.ErrorMessage)) { stringBuilder.Append(writer.ErrorMessage); } if (errors.CountRows > 0) { stringBuilder.Append(errors.DisplayByRecordNumber); } } } if (stringBuilder.Length > 0) { _MessageBox.Show(null, stringBuilder.ToString(), FileSystemUtils.GetShortDisplayFileName(fileSetting.FileName, 80), MessageBoxButtons.OK); } return(written); }