Search code examples
c#.netwinformsbackgroundworker

How to add background worker component on Windows Forms app?


I want to ask that how can I add a Background Worker component through code in a Window Form? Note that I know how to add it from ToolBox in Component tray, but I want to add it trough code. Any sample code is appreciated.


Solution

  • DoWork in a different thread, report progress to the main thread and cancel the asynchronous process are the most important functionalities in BackgroundWorker. Below example demonstrates those three functionalities quite clearly. There are heaps of examples available on the web.

    using System;
    using System.Threading;
    using System.ComponentModel;
    
    class Program
    {
      static BackgroundWorker _bw;
    
      static void Main()
      {
        _bw = new BackgroundWorker
        {
          WorkerReportsProgress = true,
          WorkerSupportsCancellation = true
        };
        _bw.DoWork += bw_DoWork;
        _bw.ProgressChanged += bw_ProgressChanged;
        _bw.RunWorkerCompleted += bw_RunWorkerCompleted;
    
        _bw.RunWorkerAsync ("Hello to worker");
    
        Console.WriteLine ("Press Enter in the next 5 seconds to cancel");
        Console.ReadLine();
        if (_bw.IsBusy) _bw.CancelAsync();
        Console.ReadLine();
      }
    
      static void bw_DoWork (object sender, DoWorkEventArgs e)
      {
        for (int i = 0; i <= 100; i += 20)
        {
          if (_bw.CancellationPending) { e.Cancel = true; return; }
          _bw.ReportProgress (i);
          Thread.Sleep (1000);      // Just for the demo... don't go sleeping
        }                           // for real in pooled threads!
    
        e.Result = 123;    // This gets passed to RunWorkerCompleted
      }
    
      static void bw_RunWorkerCompleted (object sender,
                                         RunWorkerCompletedEventArgs e)
      {
        if (e.Cancelled)
          Console.WriteLine ("You canceled!");
        else if (e.Error != null)
          Console.WriteLine ("Worker exception: " + e.Error.ToString());
        else
          Console.WriteLine ("Complete: " + e.Result);      // from DoWork
      }
    
      static void bw_ProgressChanged (object sender,
                                      ProgressChangedEventArgs e)
      {
        Console.WriteLine ("Reached " + e.ProgressPercentage + "%");
      }
    }
    

    Reference