Click here to Skip to main content
11,644,286 members (64,393 online)
Click here to Skip to main content
Add your own
alternative version

WPF Control Factory

, 20 Apr 2010 CPOL 19.3K 296 16
This article explains some advantages and disadvantages of factories, and shows one to use for generating WPF Controls.
WpfControlsAndSample.zip
WpfControlsAndSample
Pfz.ClassLibraries
Pfz
Caching
Collections
DataTypes
Extensions
Pfz.snk
Properties
Ranges
Threading
Pfz.WpfControls
BoundGridParts
Extensions
Pfz.WpfControls.snk
Properties
TypeConverters
WpfControlFactorySample
bin
Debug
Pfz.dll
Pfz.WpfControls.dll
WpfControlFactorySample.exe
Properties
Settings.settings
WpfControlFactorySample.suo
using System;
using System.Threading;
using Pfz.Threading;

namespace Pfz.Extensions.ReaderWriterLockExtensions
{
	/// <summary>
	/// Adds methods to use ReaderWriterLockSlim easily,
	/// always with time-outs to avoid dead-locks.
	/// See PfzLockConfiguration class if you want to log dead-locks.
	/// </summary>
	public static class PfzReaderWriterLockExtensions
	{
		private static readonly TimeSpan fOneSecond = new TimeSpan(0, 0, 1);
	
		#region IDisposable locks
			#region ReadLock
				/// <summary>
				/// Acquires a read-lock using the default time-out.
				/// </summary>
				public static IDisposable ReadLock(this ReaderWriterLockSlim lockObject)
				{
					return ReadLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Acquires a read-lock, using the given time-out.
				/// </summary>
				public static IDisposable ReadLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_ReadLock(lockObject);
					if (lockObject.TryEnterReadLock(timeout))
						return result;
					
					throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Read);
				}
				
				/// <summary>
				/// Tries to acquire a read-lock on the given object using the default timeout. 
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to lock.</param>
				/// <returns>A disposable object to release the lock, or null.</returns>
				public static IDisposable TryReadLock(this ReaderWriterLockSlim lockObject)
				{
					return TryReadLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Tries to acquire a read-lock on the given object using the specified timeout. 
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to lock.</param>
				/// <param name="timeout">The timeout to try for the lock.</param>
				/// <returns>A disposable object to release the lock, or null.</returns>
				public static IDisposable TryReadLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_ReadLock(lockObject);
					if (lockObject.TryEnterReadLock(timeout))
						return result;
					
					LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Read);
					return null;
				}
							
				private sealed class p_ReadLock:
					IDisposable
				{
					private ReaderWriterLockSlim fLockObject;
					
					public p_ReadLock(ReaderWriterLockSlim lockObject)
					{
						fLockObject = lockObject;
					}
					public void Dispose()
					{
						ReaderWriterLockSlim lockObject = fLockObject;
						if (lockObject != null)
						{
							lockObject.ExitReadLock();
							fLockObject = null;
						}
					}
				}
			#endregion
			#region UpgradeableLock
				/// <summary>
				/// Acquires an upgradeable-lock using the default time-out.
				/// </summary>
				public static IDisposable UpgradeableLock(this ReaderWriterLockSlim lockObject)
				{
					return UpgradeableLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Acquires an upgradeable-lock, using the given time-out.
				/// </summary>
				public static IDisposable UpgradeableLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_UpgradeableLock(lockObject);
					if (lockObject.TryEnterUpgradeableReadLock(timeout))
						return result;
					
					throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Upgradeable);
				}
				
				/// <summary>
				/// Tries to acquire an upgradeable lock on the given object, using the default timeout.
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to try to lock.</param>
				/// <returns>An disposable object to release the lock, or null if the locks fails.</returns>
				public static IDisposable TryUpgradeableLock(this ReaderWriterLockSlim lockObject)
				{
					return TryUpgradeableLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Tries to acquire an upgradeable lock on the given object, using the specified timeout.
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to try to lock.</param>
				/// <param name="timeout">The maximum time to wait for the lock.</param>
				/// <returns>An disposable object to release the lock, or null if the locks fails.</returns>
				public static IDisposable TryUpgradeableLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_UpgradeableLock(lockObject);
					if (lockObject.TryEnterUpgradeableReadLock(timeout))
						return result;
					
					LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Upgradeable);
					return null;
				}
				
				private sealed class p_UpgradeableLock:
					IDisposable
				{
					private ReaderWriterLockSlim fLockObject;
					
					public p_UpgradeableLock(ReaderWriterLockSlim lockObject)
					{
						fLockObject = lockObject;
					}
					public void Dispose()
					{
						ReaderWriterLockSlim lockObject = fLockObject;
						if (lockObject != null)
						{
							lockObject.ExitUpgradeableReadLock();
							fLockObject = null;
						}
					}
				}
			#endregion
			#region WriteLock
				/// <summary>
				/// Acquires a write-lock using the default time-out.
				/// </summary>
				public static IDisposable WriteLock(this ReaderWriterLockSlim lockObject)
				{
					return WriteLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Acquires a write-lock, using the given time-out.
				/// </summary>
				public static IDisposable WriteLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_WriteLock(lockObject);
					if (lockObject.TryEnterWriteLock(timeout))
						return result;
					
					throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Write);
				}
				
				/// <summary>
				/// Tries to acquire a write-lock on the given object using the default timeout.
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to lock.</param>
				/// <returns>A disposable object to release the lock, or null.</returns>
				public static IDisposable TryWriteLock(this ReaderWriterLockSlim lockObject)
				{
					return TryWriteLock(lockObject, LockConfiguration.DefaultLockTimeout);
				}
				
				/// <summary>
				/// Tries to acquire a write-lock on the given object using the specified timeout.
				/// If it fails, returns null.
				/// </summary>
				/// <param name="lockObject">The object to lock.</param>
				/// <param name="timeout">The maximum time to wait for the lock.</param>
				/// <returns>A disposable object to release the lock, or null.</returns>
				public static IDisposable TryWriteLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
				{
					IDisposable result = new p_WriteLock(lockObject);
					if (lockObject.TryEnterWriteLock(timeout))
						return result;
					
					LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Write);
					return null;
				}
				
				private sealed class p_WriteLock:
					IDisposable
				{
					private ReaderWriterLockSlim fLockObject;
					
					public p_WriteLock(ReaderWriterLockSlim lockObject)
					{
						fLockObject = lockObject;
					}
					public void Dispose()
					{
						ReaderWriterLockSlim lockObject = fLockObject;
						if (lockObject != null)
						{
							lockObject.ExitWriteLock();
							fLockObject = null;
						}
					}
				}
			#endregion
		#endregion
		#region Actions and AbortSafe locks
			#region ReadLock
				/// <summary>
				/// Acquires a ReadLock using the default timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void ReadLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					ReadLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}
				
				/// <summary>
				/// Acquires a ReadLock using the given timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void ReadLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (!TryReadLock(lockSlim, timeout, action))
						LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Read);
				}
				
				/// <summary>
				/// Tries to acquire a ReadLock using the default timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryReadLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					return TryReadLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}

				/// <summary>
				/// Tries to acquire a ReadLock using the given timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryReadLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (action == null)
						throw new ArgumentNullException("action");

					if (timeout < TimeSpan.Zero)
						throw new ArgumentException("timeout can't be less than zero.", "timeout");

					bool lockAcquired = false;
					try
					{
						while (timeout > TimeSpan.Zero)
						{
							try
							{
							}
							finally
							{
								TimeSpan timeOutToUse = fOneSecond;
								if (timeout < fOneSecond)
									timeOutToUse = timeout;

								lockAcquired = lockSlim.TryEnterReadLock(timeOutToUse);
							}

							if (lockAcquired)
							{
								action();

								return true;
							}

							if (AbortSafe.WasAbortRequested)
								return false;

							timeout -= fOneSecond;
						}
					}
					finally
					{
						if (lockAcquired)
							lockSlim.ExitReadLock();
					}

					return false;
				}
			#endregion
			#region UpgradeableLock
				/// <summary>
				/// Acquires a UpgradeableLock using the default timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void UpgradeableLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					UpgradeableLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}
				
				/// <summary>
				/// Acquires a UpgradeableLock using the given timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void UpgradeableLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (!TryUpgradeableLock(lockSlim, timeout, action))
						LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Upgradeable);
				}
				
				/// <summary>
				/// Tries to acquire a UpgradeableLock using the default timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryUpgradeableLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					return TryUpgradeableLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}

				/// <summary>
				/// Tries to acquire a UpgradeableLock using the given timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryUpgradeableLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (action == null)
						throw new ArgumentNullException("action");

					if (timeout < TimeSpan.Zero)
						throw new ArgumentException("timeout can't be less than zero.", "timeout");

					bool lockAcquired = false;
					try
					{
						while (timeout > TimeSpan.Zero)
						{
							try
							{
							}
							finally
							{
								TimeSpan timeOutToUse = fOneSecond;
								if (timeout < fOneSecond)
									timeOutToUse = timeout;

								lockAcquired = lockSlim.TryEnterUpgradeableReadLock(timeOutToUse);
							}

							if (lockAcquired)
							{
								action();

								return true;
							}

							if (AbortSafe.WasAbortRequested)
								return false;

							timeout -= fOneSecond;
						}
					}
					finally
					{
						if (lockAcquired)
							lockSlim.ExitUpgradeableReadLock();
					}

					return false;
				}
			#endregion
			#region WriteLock
				/// <summary>
				/// Acquires a WriteLock using the default timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void WriteLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					WriteLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}
				
				/// <summary>
				/// Acquires a WriteLock using the given timeout and then
				/// executes the given action. The lock acquisition is AbortSafe.
				/// </summary>
				public static void WriteLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (!TryWriteLock(lockSlim, timeout, action))
						LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Write);
				}
				
				/// <summary>
				/// Tries to acquire a WriteLock using the default timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryWriteLock(this ReaderWriterLockSlim lockSlim, Action action)
				{
					return TryWriteLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
				}

				/// <summary>
				/// Tries to acquire a WriteLock using the given timeout.
				/// If the lock is acquired, it executes the action and returns only
				/// after releasing the lock. If not, it returns false.
				/// The lock acquisition is AbortSafe.
				/// </summary>
				public static bool TryWriteLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
				{
					if (action == null)
						throw new ArgumentNullException("action");
					
					if (timeout < TimeSpan.Zero)
						throw new ArgumentException("timeout can't be less than zero.", "timeout");

					bool lockAcquired = false;
					try
					{
						while (timeout > TimeSpan.Zero)
						{
							try
							{
							}
							finally
							{
								TimeSpan timeOutToUse = fOneSecond;
								if (timeout < fOneSecond)
									timeOutToUse = timeout;

								lockAcquired = lockSlim.TryEnterWriteLock(timeOutToUse);
							}

							if (lockAcquired)
							{
								action();

								return true;
							}

							if (AbortSafe.WasAbortRequested)
								return false;

							timeout -= fOneSecond;
						}
					}
					finally
					{
						if (lockAcquired)
							lockSlim.ExitWriteLock();
					}

					return false;
				}
			#endregion
		#endregion
	}
}

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)

Share

About the Author

Paulo Zemek
Engineer Microsoft Corporation
United States United States
I started to program computers when I was 11 years old, as a hobbist, 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 they work easier, faster and with less errors.

Now I just started working as a Senior Software Engineer at Microsoft.

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
Microsoft MVP 2013-2014

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 20 Apr 2010
Article Copyright 2010 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid