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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:37:47+00:00 2026-06-14T14:37:47+00:00

NOTE The code below is from asp.net. If I have the (poorly written) code

  • 0

NOTE The code below is from asp.net.


If I have the (poorly written) code below

AmazonS3 s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client();

// ...
// details elided
// ...

BackgroundWorker worker = new BackgroundWorker();

worker.DoWork += new DoWorkEventHandler((s, args) =>
{
    s3Client.PutObject(titledRequest);
});

new Thread(() => worker.RunWorkerAsync()).Start();

Will the garbage collector be smart enough to never, ever collect the s3Client object until the background worker is done with it?


Note, I’m kicking off the background worker inside of a thread only in order to fix an annoying error that gets raised within asp.net that happens when I fire off the background worker directly.

  • 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-14T14:37:48+00:00Added an answer on June 14, 2026 at 2:37 pm

    Yes, it will. The compiler will generate a new class for you that contains fields for each of the locals you reference in a closure. The closure body will be emitted into a method on that class, and all of the locals in the containing function will be rewritten by the compiler to reference fields on that closure object.

    All of this magic happens at compile-time; the runtime does not need to know anything about it. Since the runtime is already smart enough not to collect an object that is the target of a delegate, the lifetimes of locals referenced by a closure are guaranteed to extend to the lifetime of the resulting delegate object.

    To illustrate, the compiler is going to spit out something like this:

    [System.Runtime.CompilerServices.CompilerGeneratedAttribute]
    internal class ClosureImplementation // See note 1
    {
        public AmazonS3 s3Client;
    
        public void Method(object s, EventArgs args)
        {
            s3Client.PutObject(titledRequest); // See note 2
        }
    }
    

    Then, in your method, this is emitted instead:

    ClosureImplementation closure = new ClosureImplementation();
    closure.s3Client = Amazon.AWSClientFactory.CreateAmazonS3Client();
    
    // ...
    
    worker.DoWork += closure.Method;
    

    Notes:

    1. The name of the generated class is chosen by the compiler; ClosureImplementation is just an example.
    2. I don’t have enough context to know where titledRequest comes from, so I intentionally did not address how the compiler will handle that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Setup: I have updated an app from ASP.NET MVC 3 to ASP.NET MVC
Note my code below. I am trying to figure out why my data is
I will post my code below as a quick note I define both of
When I run the following code, I get the exception below: ''# NOTE: ExcelApp
Note: This code actually codes from a tutorial book I'm reading at the moment,
Here is the code: //Note: x actually isn't defined, I'm pulling it from an
I have the following HTML code (note no closing tags on <\option> ) generated
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
let's say I have a html structure like this -> (note: in my code
From the below JSON, how can I retrieve title from the note and notes

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.