Solved

Custom initialization functions with IDisposable

Posted on 2011-09-10
3
293 Views
Last Modified: 2012-05-12
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;
		}
	}

}

Open in new window

0
Comment
Question by:raheelasadkhan
  • 2
3 Comments
 
LVL 15

Expert Comment

by:x77
ID: 36519945
When you call Dispose you call GC.SuppressFinalize.

This is not reversible.

Then on Domain Finalization, Gc do´nt Calls Dispose for this instance.

Your class is valid except for IDisposable Support.
Note also that Disposed member is now True.
0
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36519974
About "Not Reversible"

I inspect Gc members:

Public Shared Sub ReRegisterForFinalize(ByVal obj As Object)

Then you can undo the  GC.SuppressFinalize with Gc.ReRegisterForFinalize(myobj)

http://msdn.microsoft.com/en-us/library/system.gc.reregisterforfinalize.aspx
0
 

Author Closing Comment

by:raheelasadkhan
ID: 36711595
Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Finally almost got it 5 48
open System.IO.Compression.ZipArchiveEntry as ZipFile without storing to file 2 84
Printing 1 58
Why use this lambda? 12 33
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question