Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF Control Factory

, 20 Apr 2010
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.MonitorLockExtensions
{
	/// <summary>
	/// Adds methods to lock any object using Monitor methods easily and
	/// always with time-out, so you can avoid dead-locks.
	/// See PfzLockConfiguration class if you want to log dead-locks.
	/// </summary>
	public static class PfzMonitorLockExtensions
	{
		#region LockWithTimeout - Disposable
			/// <summary>
			/// Locks an object or throws an exception if it times-out.
			/// Use with the using keyword.
			/// Defaults to one minute.
			/// </summary>
			/// <typeparam name="T">Only used to constraint objects to reference types.</typeparam>
			/// <param name="item">The object to lock.</param>
			/// <returns>A disposable object so you can do a using in it.</returns>
			public static IDisposable LockWithTimeout<T>(this T item)
			where
				T: class
			{
				return LockWithTimeout(item, LockConfiguration.DefaultLockTimeout);
			}

			/// <summary>
			/// Locks an object or throws an exception if it times-out.
			/// Use with the using keyword.
			/// </summary>
			/// <typeparam name="T">Only used to constraint objects to reference types.</typeparam>
			/// <param name="item">The object to lock.</param>
			/// <param name="timeout">A timespan representing the timeout value.</param>
			/// <returns>A disposable object so you can do a using in it.</returns>
			public static IDisposable LockWithTimeout<T>(this T item, TimeSpan timeout)
			where
				T: class
			{
				IDisposable result = new p_LockWithTimeout(item);
				if (Monitor.TryEnter(item, timeout))
					return result;
				
				throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Monitor);
			}
			
			/// <summary>
			/// Tries to acquire a lock on the given object, using the default lock-timeout.
			/// In case of failure, it logs the error, but does not generates an exception. Instead, it returns
			/// null.
			/// </summary>
			/// <typeparam name="T">The type of class to lock.</typeparam>
			/// <param name="item">The item to lock.</param>
			/// <returns>A disposable object, so you can release the lock, or null if the lock was not acquired.</returns>
			public static IDisposable TryLockWithTimeout<T>(this T item)
			where
				T: class
			{
				return TryLockWithTimeout(item, LockConfiguration.DefaultLockTimeout);
			}
			
			/// <summary>
			/// Tries to acquire a lock on the given object, using the given time-out.
			/// In case of failure, it logs the error, but does not generates an exception. Instead, it returns
			/// null.
			/// </summary>
			/// <typeparam name="T">The type of class to lock.</typeparam>
			/// <param name="item">The item to lock.</param>
			/// <param name="timeout">The timeout value while trying to acquire the lock.</param>
			/// <returns>A disposable object, so you can release the lock, or null if the lock was not acquired.</returns>
			public static IDisposable TryLockWithTimeout<T>(this T item, TimeSpan timeout)
			{
				IDisposable result = new p_LockWithTimeout(item);
				if (Monitor.TryEnter(item))
					return result;
				
				LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Monitor);
				return null;
			}

			private sealed class p_LockWithTimeout:
				IDisposable
			{
				internal object fItem;
				public p_LockWithTimeout(object item)
				{
					fItem = item;
				}
				public void Dispose()
				{
					object item = fItem;
					
					if (item != null)
					{
						Monitor.Exit(item);
						fItem = null;
					}
				}
			}
		#endregion
		#region LockWithTimeout - Action
			private static readonly TimeSpan fOneSecond = new TimeSpan(0, 0, 1);
			
			/// <summary>
			/// Locks the specified item with the specified timeout.
			/// If it acquires the lock, runs the given action. Note that the
			/// action can be aborted, but the lock will not be held.
			/// </summary>
			public static void LockWithTimeout<T>(this T item, Action action)
			where
				T: class
			{
				LockWithTimeout(item, LockConfiguration.DefaultLockTimeout, action);
			}

			/// <summary>
			/// Locks the specified item with the specified timeout.
			/// If it acquires the lock, runs the given action. Note that the
			/// action can be aborted, but the lock will not be held.
			/// Allows you to specify the timeout value.
			/// </summary>
			public static void LockWithTimeout<T>(this T item, TimeSpan timeout, Action action)
			where
				T: class
			{
				if (!TryLockWithTimeout(item, timeout, action))
					LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Monitor);
			}
			
			/// <summary>
			/// Tries to lock an object and then execute an action.
			/// Returns if the lock was obtained and the action fully executed.
			/// Be careful, as the lock is already released when the method returns.
			/// </summary>
			public static bool TryLockWithTimeout<T>(this T item, Action action)
			where
				T: class
			{
				return TryLockWithTimeout(item, LockConfiguration.DefaultLockTimeout, action);
			}

			/// <summary>
			/// Tries to lock an object and then execute an action.
			/// Returns if the lock was obtained and the action fully executed.
			/// Be careful, as the lock is already released when the method returns.
			/// </summary>
			public static bool TryLockWithTimeout<T>(this T item, TimeSpan timeout, Action action)
			where
				T: class
			{
				return p_TryLockWithTimeout(item, timeout, action);
			}

			private static bool p_TryLockWithTimeout(object item, TimeSpan timeout, Action action)
			{
				if (action == null)
					throw new ArgumentNullException("action");

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

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

							lockAcquired = Monitor.TryEnter(item, timeOutToUse);
						}

						if (lockAcquired)
						{
							action();

							return true;
						}

						if (AbortSafe.WasAbortRequested)
							return false;

						timeout -= fOneSecond;
					}
				}
				finally
				{
					if (lockAcquired)
						Monitor.Exit(item);
				}

				return false;
			}
		#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
Architect
Canada Canada
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.
 
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
Microsoft MVP 2013

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 20 Apr 2010
Article Copyright 2010 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid