Solved

Custom initialization functions with IDisposable

Posted on 2011-09-10
3
281 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

18 Experts available now in Live!

Get 1:1 Help Now