public void CopyFile(string sourcePath, string destinationPath, IProgressMonitor monitor) { // Open the source file for reading using (var sourceStream = new FileStream(sourcePath, FileMode.Open)) { // Create the destination file for writing using (var destinationStream = new FileStream(destinationPath, FileMode.Create)) { // Copy the contents of the source file to the destination file var buffer = new byte[4096]; int bytesRead; while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0) { destinationStream.Write(buffer, 0, bytesRead); // Update the progress monitor with the percentage of the file that has been copied monitor.ReportProgress((double)destinationStream.Position / sourceStream.Length); } } } }
public void ProcessData(IEnumerableThese examples use the IProgressMonitor interface, which is part of the System.ComponentModel namespace in the .NET Standard Library. It can be used in any .NET project, including those targeting .NET Framework, .NET Core, and Xamarin.records, IProgressMonitor monitor) { int totalRecords = records.Count(); int currentRecord = 0; foreach (var record in records) { // Process the current record ProcessRecord(record); // Update the progress monitor with the current record and ETA for processing the entire dataset currentRecord++; monitor.ReportProgress($"Processing record {currentRecord} of {totalRecords} (ETA: {(totalRecords - currentRecord) * 5} seconds)"); } }