Click here to Skip to main content
15,891,874 members
Articles / Desktop Programming / WPF

WPF Control Factory

Rate me:
Please Sign up or sign in to vote.
4.25/5 (7 votes)
20 Apr 2010CPOL6 min read 37.9K   418   16  
This article explains some advantages and disadvantages of factories, and shows one to use for generating WPF Controls.
using System;
using System.IO;
using System.Threading;
using Pfz.Extensions.DisposeExtensions;
using Pfz.Extensions.StreamExtensions;

namespace Pfz.Threading
{
	/// <summary>
	/// Class with methods safe from "Thread.Abort()".
	/// </summary>
	public static class AbortSafe
	{
		/// <summary>
		/// Returns a value indicating if an abort was requested for this thread.
		/// </summary>
		public static bool WasAbortRequested
		{
			get
			{
				return (Thread.CurrentThread.ThreadState & ThreadState.AbortRequested) == ThreadState.AbortRequested;
			}
		}
	
		/// <summary>
		/// This method will make a thread that is running in non-abortable manner 
		/// (ie, inside a finally block) to skip the rest of the actual finally block
		/// if an abort was requested.
		/// </summary>
		public static void AllowAbort()
		{
			if (WasAbortRequested)
				Thread.CurrentThread.Abort();
		}
	
		/// <summary>
		/// Allocates a new object using it's default constructor and sets
		/// it's result value to the variable passed as out parameter.
		/// This will work completelly or fail completelly in case of an
		/// Abort call, so there is no risk of stopping in-the-middle of the work.
		/// </summary>
		public static void New<T>(out T variable)
		where
			T: new()
		{
			try
			{
			}
			finally
			{
				variable = new T();
			}
		}
		
		/// <summary>
		/// Runs a code block in an AbortSafe manner.
		/// Be careful when using this, as you must not avoid Aborts of long running
		/// code.
		/// </summary>
		public static void Run(Action code)
		{
			try
			{
			}
			finally
			{
				code();
			}
		}
		
		/// <summary>
		/// Runs a block of code, guaranting that:
		/// The allocation block will not be aborted.
		/// The finally block will be called, independent if the allocation block was
		/// run.
		/// The code block is the only one that could be aborted.
		/// </summary>
		public static void Run(Action allocationBlock, Action codeBlock, Action finallyBlock)
		{
			if (allocationBlock == null)
				throw new ArgumentNullException("allocationBlock");
			
			if (codeBlock == null)
				throw new ArgumentNullException("codeBlock");
			
			if (finallyBlock == null)
				throw new ArgumentNullException("finallyBlock");
		
			try
			{
				try
				{
				}
				finally
				{
					allocationBlock();
				}
				
				codeBlock();
			}
			finally
			{
				finallyBlock();
			}
		}
		
		/// <summary>
		/// Reads all bytes from a file, avoiding errors caused from Thread.Abort().
		/// </summary>
		public static byte[] ReadAllBytes(string path)
		{
			FileStream stream = null;
			try
			{
				Run(() => stream = File.OpenRead(path));
				
				int length = (int)stream.Length;
				
				byte[] bytes = new byte[length];
				stream.FullRead(bytes);
				return bytes;
			}
			finally
			{
				stream.CheckedDispose();
			}
		}
		
		/// <summary>
		/// Writes all bytes to a file, avoiding errors caused from Aborts.
		/// If an abort happens, the stream is closed and the file is deleted.
		/// </summary>
		public static void WriteAllBytes(string path, byte[] bytes)
		{
			FileStream stream = null;
			try
			{
				Run(() => stream = File.Create(path));
				stream.Write(bytes);
			}
			catch
			{
				if (stream != null)
				{
					stream.Dispose();
					stream = null;
					
					try
					{
						File.Delete(path);
					}
					catch
					{
					}
				}
				
				throw;
			}
			finally
			{
				stream.CheckedDispose();
			}
		}
		
		/// <summary>
		/// Executes the given blocks as if they where a using clause.
		/// The first block must return a disposable object. The second one will be
		/// the "body" executed inside the using clause.
		/// 
		/// It simulates:
		/// using(...allocationBlock...)
		/// {
		///		...codeBlock...
		/// }
		/// </summary>
		public static void Using<T>(Func<T> allocationBlock, Action<T> codeBlock)
		where
			T: IDisposable
		{
			if (allocationBlock == null)
				throw new ArgumentNullException("allocationBlock");
			
			if (codeBlock == null)
				throw new ArgumentNullException("codeBlock");
		
			T value = default(T);
			try
			{
				try
				{
				}
				finally
				{
					value = allocationBlock();
				}
				
				codeBlock(value);
			}
			finally
			{
				value.CheckedDispose();
			}
		}
		
		/// <summary>
		/// Locks an object and executes the given action.
		/// The lock is abort safe, not the executed block.
		/// </summary>
		public static void Lock(object objectToLock, Action action)
		{
			if (objectToLock == null)
				throw new ArgumentNullException("objectToLock");
			
			if (action == null)
				throw new ArgumentNullException("action");
		
			bool lockAcquired = false;
			try
			{
				while(true)
				{
					try
					{
					}
					finally
					{
						lockAcquired = Monitor.TryEnter(objectToLock, 1000);
					}
					
					if (lockAcquired)
					{
						action();
						return;
					}
				}
			}
			finally
			{
				if (lockAcquired)
					Monitor.Exit(objectToLock);
			}
		}
		
		/// <summary>
		/// Locks an object and executes the given action in an unabortable manner.
		/// Aborts will happen or before the lock acquisition, or after the full block
		/// is executed and the lock is released.
		/// </summary>
		public static void UnabortableLock(object objectToLock, Action action)
		{
			if (objectToLock == null)
				throw new ArgumentNullException("objectToLock");
				
			if (action == null)
				throw new ArgumentNullException("action");
			
			bool mustRun = true;	
			do
			{
				try
				{
				}
				finally
				{
					if (Monitor.TryEnter(objectToLock, 1000))
					{
						try
						{
							action();
						}
						finally
						{
							Monitor.Exit(objectToLock);
						}
						
						mustRun = false;
					}
				}
			} while(mustRun);
		}
		
		/// <summary>
		/// Acquires a read-lock and then runs the action.
		/// The lock is abort safe, not the executed block.
		/// </summary>
		public static void ReadLock(ReaderWriterLockSlim readerWriterLock, Action action)
		{
			if (readerWriterLock == null)
				throw new ArgumentNullException("readerWriterLock");
			
			if (action == null)
				throw new ArgumentNullException("action");
			
			bool lockAcquired = false;
			try
			{
				while(true)
				{
					try
					{
					}
					finally
					{
						lockAcquired = readerWriterLock.TryEnterReadLock(1000);
					}
					
					if (lockAcquired)
					{
						action();
						return;
					}
				}
			}
			finally
			{
				if (lockAcquired)
					readerWriterLock.ExitReadLock();
			}
		}

		/// <summary>
		/// Acquires an upgradeable-lock and then runs the action.
		/// The lock is abort safe, not the action.
		/// </summary>
		public static void UpgradeableLock(ReaderWriterLockSlim readerWriterLock, Action action)
		{
			if (readerWriterLock == null)
				throw new ArgumentNullException("readerWriterLock");

			if (action == null)
				throw new ArgumentNullException("action");

			bool lockAcquired = false;
			try
			{
				while (true)
				{
					try
					{
					}
					finally
					{
						lockAcquired = readerWriterLock.TryEnterUpgradeableReadLock(1000);
					}

					if (lockAcquired)
					{
						action();
						return;
					}
				}
			}
			finally
			{
				if (lockAcquired)
					readerWriterLock.ExitUpgradeableReadLock();
			}
		}

		/// <summary>
		/// Acquires a write-lock and then runs the action.
		/// The lock is abort safe, not the action.
		/// </summary>
		public static void WriteLock(ReaderWriterLockSlim readerWriterLock, Action action)
		{
			if (readerWriterLock == null)
				throw new ArgumentNullException("readerWriterLock");

			if (action == null)
				throw new ArgumentNullException("action");

			bool lockAcquired = false;
			try
			{
				while (true)
				{
					try
					{
					}
					finally
					{
						lockAcquired = readerWriterLock.TryEnterWriteLock(1000);
					}

					if (lockAcquired)
					{
						action();
						return;
					}
				}
			}
			finally
			{
				if (lockAcquired)
					readerWriterLock.ExitWriteLock();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Microsoft
United States United States
I started to program computers when I was 11 years old, as a hobbyist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.

At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do their work easier, faster and with less errors.

Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com

Codeproject MVP 2012, 2015 & 2016
Microsoft MVP 2013-2014 (in October 2014 I started working at Microsoft, so I can't be a Microsoft MVP anymore).

Comments and Discussions