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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:13:42+00:00 2026-05-25T12:13:42+00:00

In .NET (C#) I follow some custom conventions and patterns that require Constructors, Initialization

  • 0

In .NET (C#) I follow some custom conventions and patterns that require Constructors, Initialization functions and IDisposable implementations. A typical class is illustrated below. No initialization is done directly in the constructor but rather through a dedicated function that is supposed to make the object reusable. However, I am not sure what happens when Dispose gets called. If the GC calls it, the reference to the object is lost anyways so no worries there. If it is explicitly called, are there any drawbacks simply calling Initialize and treating the class as a fresh object since GC.SupressFinalize has been called? Lol, I’m sure I could have asked this in an easier way.

public abstract class Thread: System.IDisposable
{

    protected bool Disposed { get; set; }
    protected bool Terminate { get; private set; }
    public bool IsRunning { get; private set; }
    private System.Threading.Thread ThreadObject { get; set; }

    public Thread ()
    {
        this.Initialize();
    }

    ~Thread ()
    {
        this.Dispose(false);
    }

    public virtual void Initialize ()
    {
        this.Stop();

        this.Disposed = false;
        this.Terminate = true;
        this.IsRunning = false;
        this.ThreadObject = null;
    }

    //====================================================================================================
    // Functions: Thread
    //====================================================================================================

    public void Start ()
    {
        if (!this.IsRunning)
        {
            this.IsRunning = true;
            this.Terminate = false;

            this.ThreadObject = new System.Threading.Thread(new System.Threading.ThreadStart(this.Process));
            this.ThreadObject.Start();
        }
    }

    /// <summary>
    /// Override this method to do thread processing.
    /// [this.Terminate] will be set to indicate that Stop has been called.
    /// </summary>
    /// <param name="template"></param>
    protected abstract void Process ();

    public void Stop (System.TimeSpan timeout)
    {
        if (this.IsRunning)
        {
            this.Terminate = true;

            try
            {
                if (timeout.TotalMilliseconds > 1D)
                {
                    this.ThreadObject.Join(timeout);
                }
                else
                {
                    this.ThreadObject.Join();
                }
            }
            catch
            {
                try
                {
                    this.ThreadObject.Abort();
                }
                catch
                {
                }
            }

            this.ThreadObject = null;
            this.IsRunning = false;
        }
    }

    //====================================================================================================
    // Interface Implementation: System.IDisposable
    //====================================================================================================

    public void Dispose ()
    {
        this.Dispose(true);

        System.GC.SuppressFinalize(this);
    }

    protected virtual void Dispose (bool disposing)
    {
        if (!this.Disposed)
        {
            if (disposing)
            {
                // Dispose managed resources.
                this.Stop(System.TimeSpan.FromSeconds(1));
            }

            // Dispose unmanaged resources here.

            // Note disposing has been done.
            this.Disposed = true;
        }
    }

}
  • 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-25T12:13:43+00:00Added an answer on May 25, 2026 at 12:13 pm

    The GC never calls Dispose, it’s up to the consuming code. The GC does however call the finalizer. This is used in the best practice IDisposable implementation to clean up unmanaged code only.

    Where Dispose is used outside of the context of a finalizer, then there is no need for the GC to call the finalizer, and therefore SuppressFinalize is used as an optimisation to prevent it happening twice.

    If the object is reused this causes an issue. Technically you can re-register the finalizer on initialization, but this would need to be made thread safe. Common practice is that an object is not reused after it has been Disposed, and typically the Dispose method should only execute exactly once. IMO the initializer method and object reuse introduces complexities to the pattern that move it away from it’s intended purpose.

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

Sidebar

Related Questions

What are some important practices to follow when creating a .NET assembly that is
Please provide some good coding habits that one must follow to optimize vb.net code.
I have created a custom control using VB.NET in Visual Studio 2008 that gives
I wish to develop a client-server application in .NET that functions as follows: Clients
I am trying to follow some starter guides for using MEF in .Net 4,
I'm currently creating some custom helper classes, similar to ASP.NET MVC's standard HtmlHelper .
I have a class that outputs a simple report file. It reads some record
I have an ASP.NET 3.5 SP1 web application that uses a custom JavaScriptConverter. The
I've created a JS Fiddle at http://jsfiddle.net/Vcftv/2/ that illustrates the problem. I have some
This is a follow-up question to ASP.NET How to pass container value as javascript

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.