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

The Archive Base Latest Questions

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

Consider the following example public class ChildViewModel { public BackgroundWorker BackgroundWorker; public ChildViewModel() {

  • 0

Consider the following example

  public class ChildViewModel
  {
    public BackgroundWorker BackgroundWorker;

    public ChildViewModel()
    {
        InitializeBackgroundWorker();
        BackgroundWorker.RunWorkerAsync();
    }

    private void InitializeBackgroundWorker()
    {
        BackgroundWorker = new BackgroundWorker();
        BackgroundWorker.DoWork += backgroundWorker_DoWork;
        BackgroundWorker.RunWorkerCompleted +=backgroundWorker_RunWorkerCompleted;
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //do something
    }

    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        //do time consuming task
        Thread.Sleep(5000);
    }

    public void UnregisterEventHandlers()
    {
        BackgroundWorker.DoWork -= backgroundWorker_DoWork;
        BackgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
    }

}

public class ParentViewModel
{
    ChildViewModel childViewModel;

    public ParentViewModel()
    {
        childViewModel = new ChildViewModel();
        Thread.Sleep(10000);
        childViewModel = null;
        //would the childViewModel now be eligible for garbage collection at this point or would I need to unregister the background worker event handlers by calling UnregisterEventHandlers?  
    }
}

Question: Do I need to unregister the background workers event handlers for the childViewModel object to be eligible for garbage collection. (This is just an example. I am aware I can use TPL in this situation without the need for a background worker but I am curious about this specific scenario. )

  • 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-17T10:21:13+00:00Added an answer on June 17, 2026 at 10:21 am

    It depends a bit on why your backgroundworker is public. But if your ViewModel class is the only class holding a strong reference to the background worker, and the background worker is holding a hard reference to the viewmodel (via the two events), both will be marked for garbage collection once the view model is not hold by any reference anymore.

    Similar question with some more detail.

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

Sidebar

Related Questions

Consider the following example: public interface ITask { void Execute(); } public class LoggingTaskRunner
Consider the following example: class MyContainer { std::vector<void *> v; public: void Put(void *x)
Consider the following example template<class Type = void> class MyClass { public: double getValue()
Consider the following example: template <typename T> class A { public: void f() {
Consider the following example class Foo{ public: Foo(int i):x(i){} int x; }; void bar(Foo
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following example @Remote public interface RegistrationService { public String register(); public void
Consider the following simple example of code: public class TestStaticImport { static enum Branches

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.