using System; using System.Threading.Tasks; public class Program { public static async Task Main(string[] args) { var progressBar = new ProgressBar(); var progress = new Progress(value => { progressBar.Update(value); }); await LongRunningOperation(progress); } private static async Task LongRunningOperation(IProgress progress) { for (int i = 0; i < 100; i++) { await Task.Delay(50); progress.Report(i); } } } public class ProgressBar { public void Update(int value) { Console.Write($"\rProgress: {value}%"); } }
using System; using System.IO; using System.Threading.Tasks; public class Program { public static async Task Main(string[] args) { var progress = new ProgressThis example demonstrates how to log progress updates to a file using the File class. The LogProgress method writes the progress value and timestamp to a file. The LongRunningOperation method reports progress updates to the progress object passed to it. Package library: System.IO(value => { LogProgress(value); }); await LongRunningOperation(progress); } private static void LogProgress(int value) { File.AppendAllText("progress.log", $"{DateTime.Now}: Progress: {value}%\n"); } private static async Task LongRunningOperation(IProgress progress) { for (int i = 0; i < 100; i++) { await Task.Delay(50); progress.Report(i); } } }