using System.ComponentModel; using System.Net; private BackgroundWorker worker = new BackgroundWorker(); public void DownloadFileAsync(string url, string destination) { worker.DoWork += (sender, args) => { using (var client = new WebClient()) { client.DownloadFile(url, destination); } }; worker.RunWorkerAsync(); }
private BackgroundWorker calculator = new BackgroundWorker(); public void StartCalculation(int numberOfIterations) { calculator.WorkerReportsProgress = true; calculator.ProgressChanged += (_, e) => { // Update progress bar, etc. }; calculator.DoWork += (_, e) => { for (int i = 0; i < numberOfIterations; i++) { // Perform some calculation... double result = DoSomeMath(i); // Report progress to UI thread calculator.ReportProgress((i * 100) / numberOfIterations, result); } }; calculator.RunWorkerAsync(); } private double DoSomeMath(int input) { // Some complex calculation... }In this example, `BackgroundWorker` is used to perform a long-running calculation and periodically update the UI with the progress. The `calculator` object is configured to report its progress and associated results to the UI thread, using the `ReportProgress` method. To summarize, System.ComponentModel.BackgroundWorker is a package library in .NET Framework that provides a simple way to perform long-running operations asynchronously, without blocking the UI thread.