Solved

Custom initialization functions with IDisposable

Posted on 2011-09-10
3
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 96
C# Formatting DateTime String Value 5 69
Unprotect Visio drawing page in C# 4 24
Why am I losing my session variable value 11 40
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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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