C# 同步词典

/// Synchronized Dictionary
/// Dictionary that uses ReaderWriterLockSlim to syncronize all read and writes to the underlying Dictionary
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Core.Collections.Generic {
    /// <summary>
    /// Dictionary that uses ReaderWriterLockSlim to syncronize all read and writes to the underlying Dictionary
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    [Serializable]
    public class SynchronizedDictionary<TKey,TValue>:IDictionary<TKey,TValue>,IDisposable {
 
        Dictionary<TKey,TValue> _dictionary;
 
        [NonSerialized]
        ReaderWriterLockSlim _lock;
 
        public SynchronizedDictionary() {
            _dictionary = new Dictionary<TKey,TValue>();
        }
        public SynchronizedDictionary(IDictionary<TKey,TValue> dictionary) {
            _dictionary = new Dictionary<TKey,TValue>(dictionary);
        }
        public SynchronizedDictionary(IEqualityComparer<TKey> comparer) {
            _dictionary = new Dictionary<TKey,TValue>(comparer);
        }
        public SynchronizedDictionary(int capacity) {
            _dictionary = new Dictionary<TKey,TValue>(capacity);
        }
        public SynchronizedDictionary(IDictionary<TKey,TValue> dictionary,IEqualityComparer<TKey> comparer) {
            _dictionary = new Dictionary<TKey,TValue>(dictionary,comparer);
        }
        public SynchronizedDictionary(int capacity,IEqualityComparer<TKey> comparer) {
            _dictionary = new Dictionary<TKey,TValue>(capacity,comparer);
        }
        protected ReaderWriterLockSlim Lock {
            get {
                if(_lock == null) {
                    Interlocked.CompareExchange(ref _lock,new ReaderWriterLockSlim(),null);
                }
                return _lock;
            }
        }
        public void EnterReadLock() {
            Lock.EnterReadLock();
        }
        public void ExitReadLock() {
            Lock.ExitReadLock();
        }
        public void EnterWriteLock() {
            Lock.EnterWriteLock();
        }
        public void ExitWriteLock() {
            Lock.ExitWriteLock();
        }
        public void EnterUpgradeableReadLock() {
            Lock.EnterUpgradeableReadLock();
        }
        public void ExitUpgradeableReadLock() {
            Lock.ExitUpgradeableReadLock();
        }
        protected Dictionary<TKey,TValue> Dictionary {
            get {
                return _dictionary;
            }
        }
        public TValue GetAdd(TKey key,Func<TValue> addfunction) {
 
            if(addfunction == null)
                throw new ArgumentNullException("Func<TValue> addfunction");
 
            try {
                EnterUpgradeableReadLock();
 
                if(!_dictionary.ContainsKey(key)) {
                    try {
                        EnterWriteLock();
 
                        TValue value = addfunction();
 
                        _dictionary.Add(key,value);
                        return value;
                    }
                    finally {
                        ExitWriteLock();
                    }
                }
                else {
                    return _dictionary[key];
                }
            }
            finally {
                ExitUpgradeableReadLock();
            }
        }
        public void Add(TKey key,TValue value) {
            try {
                EnterWriteLock();
                _dictionary.Add(key,value);
            }
            finally {
                ExitWriteLock();
            }
        }
        public void Add(KeyValuePair<TKey,TValue> item) {
            try {
                EnterWriteLock();
                _dictionary.Add(item.Key,item.Value);
            }
            finally {
                ExitWriteLock();
            }
        }
        public bool Add(TKey key,TValue value,bool throwOnNotFound) {
            try {
                EnterUpgradeableReadLock();
                if(!_dictionary.ContainsKey(key)) {
                    try {
                        EnterWriteLock();
                        _dictionary.Add(key,value);
                        return true;
                    }
                    finally {
                        ExitWriteLock();
                    }
                }
                else {
                    if(throwOnNotFound)
                        throw new ArgumentNullException();
                    else
                        return false;
                }
            }
            finally {
                ExitUpgradeableReadLock();
            }
        }
        public bool ContainsKey(TKey key) {
            try {
                EnterReadLock();
                return _dictionary.ContainsKey(key);
            }
            finally {
                ExitReadLock();
            }
        }
        public bool Contains(KeyValuePair<TKey,TValue> item) {
            try {
                EnterReadLock();
                return _dictionary.Contains(item);
            }
            finally {
                ExitReadLock();
            }
        }
        public TKey[] KeysToArray() {
            try {
                EnterReadLock();
                return _dictionary.Keys.ToArray();
            }
            finally {
                ExitReadLock();
            }
        }
        public ICollection<TKey> Keys {
            get {
                return _dictionary.Keys;
            }
        }
        public bool Remove(TKey key) {
            try {
                EnterWriteLock();
                return _dictionary.Remove(key);
            }
            finally {
                ExitWriteLock();
            }
        }
        public bool Remove(KeyValuePair<TKey,TValue> item) {
            try {
                EnterWriteLock();
                return _dictionary.Remove(item.Key);
            }
            finally {
                ExitWriteLock();
            }
        }
        public bool TryGetValue(TKey key,out TValue value) {
            try {
                EnterReadLock();
                return _dictionary.TryGetValue(key,out value);
            }
            finally {
                ExitReadLock();
            }
        }
        public TValue[] ValuesToArray() {
            try {
                EnterReadLock();
                return _dictionary.Values.ToArray();
            }
            finally {
                ExitReadLock();
            }
        }
        public ICollection<TValue> Values {
            get {
                return _dictionary.Values;
            }
        }
 
        public TValue this[TKey key] {
            get {
                try {
                    EnterReadLock();
                    return _dictionary[key];
                }
                finally {
                    ExitReadLock();
                }
            }
            set {
                try {
                    EnterWriteLock();
                    _dictionary[key] = value;
                }
                finally {
                    ExitWriteLock();
                }
            }
        }
        public void Clear() {
            try {
                EnterWriteLock();
                _dictionary.Clear();
            }
            finally {
                ExitWriteLock();
            }
        }
        public void CopyTo(KeyValuePair<TKey,TValue>[] array,int arrayIndex) {
            try {
                EnterReadLock();
 
                for(int i = 0;i < _dictionary.Count;i++) {
                    array.SetValue(_dictionary.ElementAt(i),arrayIndex);
                }
            }
            finally {
                ExitReadLock();
            }
        }
        public int Count {
            get {
                try {
                    EnterReadLock();
                    return _dictionary.Count;
                }
                finally {
                    ExitReadLock();
                }
            }
        }
        public bool IsReadOnly {
            get {
                return false;
            }
        }
        public bool IsSynchronized {
            get {
                return true;
            }
        }
        public IEnumerator<KeyValuePair<TKey,TValue>> GetEnumerator() {
            EnterReadLock();
            try {
                return _dictionary.GetEnumerator();
            }
            finally {
                ExitReadLock();
            }
        }
        IEnumerator IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }
 
        protected bool IsDisposed { get; set; }
        public virtual void Dispose() {
            if(IsDisposed)
                throw new ObjectDisposedException(this.GetType().Name);
            try {
                this.Dispose(true);
            }
            finally {
                GC.SuppressFinalize(this);
            }
        }
        protected virtual void Dispose(bool disposing) {
            try {
                if(!IsDisposed) {
                    if(disposing) {
                        if(_lock != null) {
                            _lock.Dispose();
                            _lock = null;
                        }
                    }
                }
            }
            finally {
                this.IsDisposed = true;
            }
        }
        //Only add Finalizer in you need to dispose of resources with out call Dispose() directly
        ~SynchronizedDictionary() {
            Dispose(!IsDisposed);
        }
    }
}

C# Singleton与事件处理程序

    public class SingletonClassName
    {
        #region EventArg Declarations

        public class SingletonClassNameEventArgs : EventArgs
        {
            #region Properties

            public PropertyDataType PropertyName { get; set; }

            #endregion



            #region Constructors

            public SingletonClassNameEventArgs(PropertyDataType _PropertyName)
            {
                this.PropertyName = _PropertyName;
            }

            #endregion
        }

        #endregion



        #region Variable Declarations

        private static readonly SingletonClassName _instance = new SingletonClassName();
        private static object _threadSynchronizationHandle = null;

        #endregion



        #region Constructors

        //Note that we are making the constructor private.  Doing so ensures that the class cannot
        //be directly instantiated which is one of the principles of the Singleton pattern.
        private SingletonClassName()
        {
        }

        #endregion



        #region Singleton Access Point

        public static SingletonClassName Instance
        {
            get
            {
                lock (GetThreadSynchronizationHandle())
                {
                    return _instance;
                }
            }
        }

        #endregion



        #region Thread Synchronization Handles

        private static object GetThreadSynchronizationHandle()
        {
            //When the thread synchronization handle object is requested, we use the CompareExchange
            //method of the Interlocked class to see if the value is null.  If it is null, then we 
            //will create the new object.  If it is not null then we will return the previously
            //allocated lock target.
            Interlocked.CompareExchange(ref _threadSynchronizationHandle, new object(), null);
            return _threadSynchronizationHandle;
        }

        #endregion



        #region Public Event Handlers

        public event EventHandler<SingletonClassNameEventArgs> OnRaiseEventNameEvent;

        #endregion



        #region Public Raise Event Handlers

        public void RaiseEventNameEvent(SingletonClassNameEventArgs e)
        {
            //Note that we are making a temporary copy of the event to avoid the possibility of a race 
            //condition if the last subscriber of the event unsubscribes immediately after
            //the null check and before the actual event is raised.  
            EventHandler<SingletonClassNameEventArgs> eventHandler = OnRaiseEventNameEvent;



            //We will only raise the event if there are subscribers to the event.
            if (eventHandler != null)
            {
                eventHandler(this, e);
            }
        }

        #endregion



        #region Public Methods

        #endregion



        #region Private Methods

        #endregion
    }

CSS CSS - CSS3 border-radius - 理解CSS3 Moz Mozilla KHTML Webkit Border Radius.css

/*
	201008051249 - brandonjp - me trying to remember css/moz/webkit border radius
	Thanks to Jacob Bijani - CSS Border Radius http: //bit.ly/8Y8cJT
	Thanks to CSS Border Radius : : Richard A. Johnson http: //bit.ly/c9FclS
*/


CSS3 {
	border-top-left-radius: 22px;
	border-top-right-radius: 44px;
	border-bottom-right-radius: 66px;
	border-bottom-left-radius: 88px;
}

MOZILLA {
	-moz-border-radius-topleft: 22px;
	-moz-border-radius-topright: 44px;
	-moz-border-radius-bottomright: 66px;
	-moz-border-radius-bottomleft: 88px;
}

KHTML {
	-khtml-border-top-left-radius: 22px;
	-khtml-border-top-right-radius: 44px;
	-khtml-border-bottom-right-radius: 66px;
	-khtml-border-bottom-left-radius: 88px;
}

WEBKIT {
	-webkit-border-top-left-radius: 22px;
	-webkit-border-top-right-radius: 44px;
	-webkit-border-bottom-right-radius: 66px;
	-webkit-border-bottom-left-radius: 88px;
}



TOP-LEFT {
	border-top-left-radius: 22px;
	-moz-border-radius-topleft: 22px;
	-khtml-border-top-left-radius: 22px;
	-webkit-border-top-left-radius: 22px;
}

TOP-RIGHT {
	border-top-right-radius: 44px;
	-moz-border-radius-topright: 44px;
	-khtml-border-top-right-radius: 44px;
	-webkit-border-top-right-radius: 44px;
}

BOTTOM-RIGHT {
	border-bottom-right-radius: 66px;
	-moz-border-radius-bottomright: 66px;
	-khtml-border-bottom-right-radius: 66px;
	-webkit-border-bottom-right-radius: 66px;
}

BOTTOM-LEFT {
	border-bottom-left-radius: 88px;
	-moz-border-radius-bottomleft: 88px;
	-khtml-border-bottom-left-radius: 88px;
	-webkit-border-bottom-left-radius: 88px;
}



CSS 3 {
/* 5px radius on all 4 corners of the table */
	border-radius: 5px;

/* 5px radius on top left and bottom right corners only */
	border-radius: 5px 0 5px 0;

/* 5px radius on bottom left and top right corners only */
	border-radius: 0 5px 0 5px;

/* 5px radius on the top left corner only */
	border-top-left-radius: 5px;
/* 5px radius on the bottom left corner only */
	border-bottom-left-radius: 5px;
/* 5px radius on the top right corner only */
	border-top-right-radius: 5px;
/* 5px radius on the bottom right corner only */
	border-bottom-right-radius: 5px;
}

Mozilla (Firefox) {
/* 5px radius on all 4 corners of the table */
	-moz-border-radius: 5px;

/* 5px radius on top left and bottom right corners only */
	-moz-border-radius: 5px 0 5px 0;

/* 5px radius on bottom left and top right corners only */
	-moz-border-radius: 0 5px 0 5px;

/* 5px radius on the top left corner only */
	-moz-border-radius-topleft: 5px;
/* 5px radius on the bottom left corner only */
	-moz-border-radius-bottomleft: 5px;
/* 5px radius on the top right corner only */
	-moz-border-radius-topright: 5px;
/* 5px radius on the bottom right corner only */
	-moz-border-radius-bottomright: 5px;
}

KHTML (Konqueror) {
/* add -khtml- in front of the CSS 3 styles */
	-khtml-border-radius: 5px;
}

Webkit (Safari/Chrome) {
/* add -webkit- in front of the CSS 3 styles */
	-webkit-border-top-right-radius: 5px;
}

HTML 每3秒刷新一次浏览器

<meta http-equiv="refresh" content="3" />

Bash Tar.gz和Untar.gz

# Tar
tar -czf filename.tar.gz dir

# Untar
tar xfz filename.tar.gz

CSS CSS3:颜色和背景过渡效果

div.class a:hover  { 
	-o-transition-duration: .55s;
	-o-transition-property: color, background-color;
	-webkit-transition-duration: .55s;
	-webkit-transition-property: color, background-color;
        -moz-transition-duration: .55s;
	-moz-transition-property: color, background-color; }

PHP 将Zend Framework 1.10与Doctrine 2集成(使用Doctrine的Autoloader)

function _initDoctrine() {
	// setup Zend & Doctrine Autoloaders
	require_once "Doctrine/Common/ClassLoader.php";

	$zendAutoloader = Zend_Loader_Autoloader::getInstance();

	// $autoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');

	$autoloader = array(new \Doctrine\Common\ClassLoader('Symfony'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Symfony\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Models', realpath(__DIR__ . '/..')), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Application\\Models\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Proxies', realpath(__DIR__ . '/..')), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Application\\Proxies');
	$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');

	// setup configuration as seen from the sandbox application
	// TODO: read configuration from application.ini
	$config = new \Doctrine\ORM\Configuration;
	$cache = new \Doctrine\Common\Cache\ArrayCache;
	$config->setMetadataCacheImpl($cache);
	$driverImpl = $config->newDefaultAnnotationDriver(realpath(__DIR__ . '/models'));
	$config->setMetadataDriverImpl($driverImpl);
	$config->setQueryCacheImpl($cache);
	$config->setProxyDir(realpath(__DIR__ . '/proxies'));
	$config->setProxyNamespace('Application\\Proxies');
	$config->setAutoGenerateProxyClasses(true);

	$connectionOptions = array(
	  'driver' => 'pdo_mysql',
	  'user' => 'root',
	  'password' => '',
	  'dbname' => 'learningzf'
	);

	// setup entity manager
	$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
	Zend_Registry::set("em", $em);
	return $em;
}

CSS Css li图标化

The first list item uses the general attribute selector to look for any tags with an accesskey attribute and display an icon to alert users to its presence:

#test_selectors a[accesskey] {
background: url('icon_key.gif') no-repeat 0 50%;
text-indent: 20px;
}

The second item looks for links with a type attribute value of ‘application/pdf’ (you do mark up links to documents using the type attribute, right?) using the exact attribute selector, then inserts an icon advising you of the destination document:

#test_selectors a[type='application/pdf'] {
background: url('file_acrobat.gif') no-repeat 0 50%;
text-indent: 20px;
}

Finally, if you have a multi-language site you can link to another version using the lang attribute with the value of that language, then use the language attribute selector to apply that country flag:

#test_selectors a[lang|='fr'] {
background: url('fr.gif') no-repeat 0 50%;
text-indent: 20px;
}

jQuery 使用jQuery简单地返回页面

$(document).ready(function() {

  $('.backlink').click(function() {
	
     history.back()
		
    });
	
});

CSS 放下第一个后者

p:first-letter{
	display:block;
	margin:5px 0 0 5px;
	float:left;
	color:#FF3366;
	font-size:60px;
	font-family:Georgia;
}