Solved

Custom initialization functions with IDisposable

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now