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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:43:39+00:00 2026-05-10T21:43:39+00:00

consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker

  • 0

consider this code block

public void ManageInstalledComponentsUpdate()         {             IUpdateView view = new UpdaterForm();             BackgroundWorker worker = new BackgroundWorker();             Update update = new Update();             worker.WorkerReportsProgress = true;             worker.WorkerSupportsCancellation = true;             worker.DoWork += new DoWorkEventHandler(update.DoUpdate);             worker.ProgressChanged += new ProgressChangedEventHandler(view.ProgressCallback);             worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(view.CompletionCallback);                         worker.RunWorkerAsync();             Application.Run(view as UpdaterForm);              } 

It all works great but I want to understand why the objects (worker,view and update) don’t get garbage collected

  • 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. 2026-05-10T21:43:40+00:00Added an answer on May 10, 2026 at 9:43 pm

    Threads count as root objects; I don’t know exactly how BackgroundWorker operates, but it seems likely that the primary thread method is going to access state on the worker instance; as such, the worker thread itself will keep the BackgroundWorker instance alive until (at least) the thread has exited.

    Of course; collection also requires that all other (live) objects have de-referenced the worker object; note also that collection of stack variables can be different in debug/release, and with/without a debugger attached.

    [edit] As has also been noted; the event handlers on the worker (in your code) will keep the ‘view’ and ‘update’ objects alive (via the delegate), but not the other way around. As long as the worker has a shorter life than the ‘view’ and ‘update’, you don’t need to get paranoid about unsubscribing the events. I’ve edited the code to include a ‘SomeTarget’ object that isonly referenced by the worker: you should see this effect (i.e. the target dies with the worker).

    Re worker getting collected when the thread dies: here’s the proof; you should see ‘worker finalized’ after the worker reports exit:

    using System; using System.ComponentModel; using System.Threading; using System.Windows.Forms; class Demo : Form {     class ChattyWorker : BackgroundWorker     {         ~ChattyWorker()         {             Console.WriteLine('Worker finalized');         }     }     class SomeTarget     {         ~SomeTarget()         {             Console.WriteLine('Target finalized');         }         public SomeTarget()         {             Console.WriteLine('Target created');         }         public void Foo(object sender, EventArgs args)         {             Console.WriteLine('Foo');         }     }     static void Collect(object sender, EventArgs args)     {         Console.WriteLine('Collecting...');         GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);     }     protected override void OnLoad(EventArgs e)     {         base.OnLoad(e);          System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();         timer.Interval = 100;         timer.Tick += Collect;         timer.Start();          ChattyWorker worker = new ChattyWorker();         worker.RunWorkerCompleted += new SomeTarget().Foo;         worker.DoWork += delegate         {             Console.WriteLine('Worker starting');             for (int i = 0; i < 10; i++)             {                 Thread.Sleep(250);                 Console.WriteLine(i);             }             Console.WriteLine('Worker exiting');         };         worker.RunWorkerAsync();     }     [STAThread]     static void Main()     { // using a form to force a sync context         Application.Run(new Demo());     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 51k
  • Answers 51k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer i hate to be a purist; but i think that… May 11, 2026 at 6:20 am
  • added an answer It is a way of specifying the number of bits… May 11, 2026 at 6:20 am
  • added an answer You can use a box enclosing the message send arrow… May 11, 2026 at 6:20 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.