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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:23:36+00:00 2026-05-14T18:23:36+00:00

I have a simple class: public class Runner { public void RunAndForget(RunDelegate method) {

  • 0

I have a simple class:

public class Runner
{
    public void RunAndForget(RunDelegate method)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(Run), method);
    }

    private void Run(object o)
    {
        ((RunDelegate )o).Invoke();
    }
}

And if I use this like so:

private void RunSomethingASync()
{
    Runner runner = new Runner();
    runner.FireAndForget(new RunDelegate(Something));
}

Is there any danger using it like this? My C++ guts tell me that runner object should be destroyed after RunSomethingASync is finished. Am I right? What happens then to the method running on different thread?

Or perhaps it is other way around and runner will not be collected? That would be a problem considering I may call RunSomethingASync() many times.

  • 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-05-14T18:23:38+00:00Added an answer on May 14, 2026 at 6:23 pm

    My C++ guts tell me that runner object should be destroyed after RunSomethingASync is finished. Am I right?

    In short, no. .NET garbage collection is not done on a deterministic basis. An object is eligible for garbage collection once it is no longer rooted – that is, once no more reference paths to it (from living objects) exist. There is no guarantee that any particular object — or, indeed, any object at all — will ever be garbage collected, though it’s pretty likely.

    However, in your case, your object isn’t even eligible. While the object is falling out of scope in your code (so it would appear that no references exist), delegates do contain references to their owning instance (assuming they point to an instance method). A reference to your object will exist until Run is called, as the ThreadPool is holding on to a reference to the delegate until then, and that delegate has a reference to Runner.

    So, to answer your question, there is no danger in what you’re doing. Because the delegate references the instance, your object cannot and will not be collected until after your operation completes. Once it completes, your object will then be eligible.

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

Sidebar

Related Questions

I have a simple class: class A { public: int get() const; private: void
I have a simple class like this: class A { public: void f(const int&
I have a simple class XmlFileHelper as follows: public class XmlFileHelper { #region Private
I have this simple class: public class LuceneUtil{ private final EntityManager entityManager; public LuceneUtil()
I have this simple class: public class MappingCollection<T> : List<T> { private int _declaredTotal
I currently have a very simple class: public class Music { private Sequence sequence;
I have a simple class CustomQuoteRequest: public class CustomQuoteRequest { private String requestId; private
I have this simple class public class Position{ int x; int y; Position(int x,int
I have a simple class inheriting from WebControl as follow: public class Instrument :
If I have a simple class such as:- @XmlRootElement public class MyClass { @XmlAttribute(required=true)

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.