using System; using System.Threading; public static void Main(string[] args) { var progress = new Progress(value => Console.WriteLine("Progress: {0}%", value)); var cancellationTokenSource = new CancellationTokenSource(); var cancellationToken = cancellationTokenSource.Token; var task = Task.Run(() => DoWork(progress, cancellationToken), cancellationToken); Thread.Sleep(5000); // simulate some work being done cancellationTokenSource.Cancel(); task.Wait(); Console.WriteLine("Task ended"); } private static void DoWork(IProgress progress, CancellationToken cancellationToken) { for (int i = 0; i < 100; i++) { if (cancellationToken.IsCancellationRequested) break; progress.Report(i); Thread.Sleep(100); // simulate work being done } }
using System.Diagnostics; using System.Threading; public static void Main(string[] args) { var progress = new ProgressThis example demonstrates how to use the IProgressMonitor interface to monitor the progress of a long-running task again. The DoWork method performs some work and periodically reports the progress using the Report method of the progress object. The main method starts the task and waits for it to complete. Package library: System.Diagnostics.Process In conclusion, the IProgressMonitor EndTask interface is a powerful feature that enables developers to monitor the progress of long-running tasks in real-time. The interface is used in many libraries and frameworks to provide users with feedback on the status of a task in progress. The package library that implements this interface may vary depending on the context of the application or system being developed.(value => Debug.WriteLine("Progress: {0}%", value)); var task = Task.Run(() => DoWork(progress)); task.Wait(); } private static void DoWork(IProgress progress) { for (int i = 1; i <= 100; i++) { progress.Report(i / 100.0); Thread.Sleep(100); // simulate work being done } }