Solved

Custom initialization functions with IDisposable

Posted on 2011-09-10
3
295 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

763 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