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 9119243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:20:59+00:00 2026-06-17T05:20:59+00:00

I have a form. The form has a logic class that does something. I

  • 0

I have a form. The form has a logic class that does something. I want the form to be responsive while this something is done so I create a BackgroundWorker object.
I also want to get updates from the logic class to the form. I thought to use the progress update event of the BackgroundWorker.

So I subscribe a method on the form to to worker.ProgressChanged.

public class MyForm : Form
{
    private LogicClass logicObject;

    public void StartOperation()
    {
        BGWorkerBase worker = new BGWorkerBase(logicObject);
        worker.DoWork += new DoWorkEventHandler(DoOperation);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(OperationComplete);
        worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
        worker.RunWorkerAsync();
    }

    public void DoOperation()
    {
        logicObject.DoSomething();
    }

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Update form
    }
}

I created a custome Backgroundworker which subscribes to the update event of the logic class and invokes the ProgressChanged event:

public class BGWorkerBase : BackgroundWorker
{
    public BGWorkerBase(LogicClass logicObject)
    {
        // connection is the logic class. it has an update event
        logicObject.UpdatePublished += new EventHandler(connection_UpdatePublished);
        this.WorkerReportsProgress = true;
    }
    // Here I invoke the backgroundworker OnProgressChangedEvent
    private void connection_UpdatePublished(object sender, EventArgs e)
    {
        OnProgressChanged(new ProgressChangedEventArgs(0, e));
    }
}

The problem is that as I trigger the operation repeatedly, the update is being published multiple times. I checked and found that the connection object has multiple workers subscribed to the publish event. So it seems I created 2 problems here:
1. A single event is published multiple times.
2. I am keeping a reference in the connection object to backgroundworkers which should have been disposed of once they finished their job.

What would be the best way to fix this? I may be able to remove the subscription of the worker when its completing its job, but it seems dirty. Is there a better way to manage this situation?

  • 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-17T05:21:01+00:00Added an answer on June 17, 2026 at 5:21 am

    You should unsubscribe from the event when the worker has finished. I.e. somewhere in your code you should add the following:

    logicObject.UpdatePublished -= connection_UpdatePublished;
    

    This will remove your event handler from the list of delegates registered to handle the UpdatePublished event.

    This SO question/answer explains how you may accidentally create memory leaks using events:
    Why and How to avoid Event Handler memory leaks?

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

Sidebar

Related Questions

I want to create a gridview form a class and send that gridview as
I want create one form from another. But the Form class has no the
I have a Winform app that has a backgroundWorker thread and does all operations
I have a login form that has Username and password as inputs from user
I have a form, this form has a submit and a cancel button. when
I have a form that has label values that I would like to pass
I have a form which has only tab control in it. I want to
I have a form that has a group of 3 text_fields. I need two
I have to implement an Form View, or in other words: A class that
I have a windows form which has 1 button (for simplicity). On clicking this

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.