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

  • Home
  • SEARCH
  • 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 9189187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:11:15+00:00 2026-06-17T20:11:15+00:00

I am experimenting with BackgroundWorker, and trying to notify my main Thread per an

  • 0

I am experimenting with BackgroundWorker, and trying to notify my main Thread per an Event. These things are all new to me, and wanted to ask, if I am doing it OK.

I simplified a winforms problem of mine, in the following way: (It has only 1 Button, and counts to ten in another Thread when I press it)

public partial class Form1 : Form
{
    public void Subscribe(CountToTen c)
    {
        c.HandleWorkerEvent += new CountToTen.WorkerHandler(OtherThreadFinished);
    }
    private void OtherThreadFinished(CountToTen c, EventArgs e)
    {
        Debug.WriteLine("I'm ready !!!");
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void btn_do_Click(object sender, EventArgs e)
    {
        CountToTen newThread = new CountToTen();
        Subscribe(newThread);
        newThread.StartCountingAndReportIfFinished();
    }
}

CountToTen class:

public class CountToTen
{

    public event WorkerHandler HandleWorkerEvent;
    public EventArgs e;
    public delegate void WorkerHandler(CountToTen c, EventArgs e);
    public void StartCountingAndReportIfFinished()
    {
        BackgroundWorker worker = new BackgroundWorker();

        worker.DoWork += delegate(object s, DoWorkEventArgs args)
        {
            for (int i = 1; i <= 10; i++)
            {
                Thread.Sleep(300);
                Debug.WriteLine("Counting :" + i.ToString());

            }
        };

        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs args)
            {
                Debug.WriteLine("Fromt Thread2 : I am finished!");
                if (HandleWorkerEvent != null)
                {
                    HandleWorkerEvent(this, e);
                }
            });

        worker.RunWorkerAsync();
        worker.Dispose();
    }
}

I was trying to create an event, when the BW is finished, and subscribe to this event in my main form.
It works fine, but, I do not really understand what happens in this line:

worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs args)
            {
                Debug.WriteLine("Fromt Thread2 : I am finished!");
                if (HandleWorkerEvent != null)
                {
                    HandleWorkerEvent(this, e);
                }
            });

Am I not creating an event here for my BW, when it is finished, and then call the another for the main thread? Is it not overkill? Could I subscribe directly to the RunWorkerCompleteEventHandler as well?

I’m a bit confused here, please enlighten a beginner.
Thanks

  • 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-06-17T20:11:17+00:00Added an answer on June 17, 2026 at 8:11 pm
    Am I not creating an event here for my BW, when it is finished,
     and then call the another for the main thread?
    

    Yes.. You are handling the events of BackgroundWorker in your CountToTen class. And Obviously as per your class architecture the only way to notify your mainform through Event.

      Is it not overkill? Could I subscribe directly to the RunWorkerCompleteEventHandler
      as well? 
    

    Ofcourse you can..Your BackgroundWorker is not exposed directly to the mainform else you can subscribe the RunWorkerCompletedEvent from there itself.

    Note:
    You don’t have to call Dispose(). It implements the IDisposable interface via Component.
    For detail see here

    Update

    • Alternative way

      public partial class Form1 : Form
      {
        private void btn_do_Click(object sender, EventArgs e)
        { 
          CountToTen obj= new CountToTen();
           obj.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
           obj.bw.RunWorkerAsync();
         }
      
         void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){}       
      }
      class CountToTen 
      {
      public BackgroundWorker bw = new BackgroundWorker();
      public CountToTen()
      {
          bw.DoWork += new DoWorkEventHandler(bw_DoWork);
      }
      
      void bw_DoWork(object sender, DoWorkEventArgs e)
      {
          //Do your Stuff
      }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Experimenting with Scala... I'm trying to define something analogous to the @ hack in
I am experimenting with Dart. I have created an on click event like so:
Experimenting with Spring-JDBC. I am using this as reference. I am trying to get
Experimenting with new features of T-SQL, I've run into a puzzle. Here is some
Im experimenting with the espn public API and am trying to use their json
I'm experimenting some difficulties trying to use Connection String Builders (ADO.NET) within LINQ to
Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
After experimenting with composite operations and drawing images on the canvas I'm now trying
Im experimenting with the following idea. I am trying to build a fallback system
Experimenting with jQuery and trying to make a small slide show that rotates through

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.