Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6577895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:41:55+00:00 2026-05-25T15:41:55+00:00

I want to ask that how can I add a Background Worker component through

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:41:56+00:00Added an answer on May 25, 2026 at 3:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know that how can I add NSMutableArray in to an NSMutableArray
I want to ask how to add a computed column in BIRT that compute
i am new to iPhone programming i want to ask that whether i can
I just wanna ask how can I make a form with two buttons that
I know about that we can add facts dynamic at run time, mean fact1(+First,+Second).
Yes, as you might guess, I want to ask about Cloud-computing. I doubt that
I am not even sure how to ask this question. I want something that
I want to ask in which phase of Control Execution Lifecycle is the Server-Side-Code
I want to ask how can someone read keystrokes while in a html page
I want to ask a question about the iPhone application. The application can let

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.