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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:49:06+00:00 2026-06-05T21:49:06+00:00

Can I define a background worker in a method ? private void DownLoadFile(string fileLocation){

  • 0

Can I define a background worker in a method ?

private void DownLoadFile(string fileLocation){
  BackgroundWorker worker = new BackgroundWorker();

  worker.DoWork += new DoWorkEventHandler((obj, args) => { 
      // Will be executed by back ground thread asynchronously.
      args.Result = Download(fileLocation);   
  });

  worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((obj, args) => { 
      // will be executed in the main thread.
      Result r = args.Result as Result;
      ReportResult(r);
   });

   worker.RunWorkerAsync(fileLocation);
}

Question: If Download() function takes a long time to download the file, can GC kick in and collect worker object before the RunWorkerCompleted() is executed ?

  • 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-05T21:49:08+00:00Added an answer on June 5, 2026 at 9:49 pm

    Given that you’re not actually using much of the functionality of BackgroundWorker, I would recommend using the TPL for this instead:

    private void DownLoadFile(string fileLocation)
    {
        Task.Factory.StartNew( () => Download(fileLocation))
            .ContinueWith(t => ReportResult(t.Result), TaskScheduler.FromCurrentSynchronizationContext());
    }
    

    That being said, the worker object will not be garbage collected once it’s running, as the ThreadPool thread itself will keep worker as a “used object”. The garbage collector will not be able to collect it until after the completion event handler executes, at which point in time there would be no user code which could reach the instance of BackgroundWorker.

    In addition, it will likely keep the instance of this class from being garbage collected, as the instance methods (ReportResults) used by the closure keep the instance of “this” accessible and not eligible for GC.

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

Sidebar

Related Questions

I know I can define new structs in ruby by doing Person = Struct.new(:first_name,
As I know I can define a new Enum DataFormat at the my project
how can I define and use local fonts on the server? exec(Init::$static['path_imagemagick'].'convert -background none
Private _bgWorker As BackgroundWorker = Nothing Private _bgWorkerMessage As String = String.Empty Private _bgPercentComplete
I can define default value in domain by this way : class ProcessingPriority {
I can define church numerals fairly easy using scheme: > (define f (lambda (x)
I know I can define default parameters in python, but can I do so
In HTML/CSS you can define a style which can be applied to many types
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
In C# you can define delegates anonymously (even though they are nothing more than

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.