using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;
namespace CYQ.Data.Tool
{
///
/// 线程安全字典类
///
/// key
/// value
public partial class MDictionary : Dictionary
{
private static readonly object lockObj = new object();
public MDictionary()
: base()
{
}
public MDictionary(IEqualityComparer comparer)
: base(comparer)
{
}
public MDictionary(int capacity)
: base(capacity)
{
}
public MDictionary(int capacity, IEqualityComparer comparer)
: base(capacity, comparer)
{
}
public new void Add(K key, V value)
{
Add(key, value, 1);
}
private void Add(K key, V value, int times)
{
try
{
lock (lockObj)
{
base.Add(key, value);
}
}
catch (Exception err)
{
if (!ContainsKey(key))
{
if (times > 3)
{
Log.WriteLogToTxt(err);
return;
}
else if (times > 2)
{
System.Threading.Thread.Sleep(10);
}
times++;
Add(key, value, times);
}
}
}
public new void Remove(K key)
{
Remove(key, 1);
}
private void Remove(K key, int times)
{
try
{
lock (lockObj)
{
base.Remove(key);
}
}
catch (Exception err)
{
if (ContainsKey(key))
{
if (times > 3)
{
Log.WriteLogToTxt(err);
return;
}
else if (times > 2)
{
System.Threading.Thread.Sleep(10);
}
times++;
Remove(key, times);
}
}
}
///
/// 索引取值
///
///
///
public new V this[K key]
{
get
{
lock (lockObj)
{
if (base.ContainsKey(key))
{
return base[key];
}
}
return default(V);
}
set
{
base[key] = value;
}
}
///
/// 通过index索引取值
///
///
public V this[int index]
{
get
{
if (index >= 0 && index < this.Count)
{
lock (lockObj)
{
int i = 0;
foreach (V value in this.Values)
{
if (i == index)
{
return value;
}
i++;
}
}
}
return default(V);
}
set
{
if (index >= 0 && index < this.Count)
{
lock (lockObj)
{
int i = 0;
foreach (K key in this.Keys)
{
if (i == index)
{
this[key] = value;
break;
}
i++;
}
}
}
}
}
///
/// 当Key为int时,通过此方法取值
///
///
///
public V Get(K key)
{
lock (lockObj)
{
if (base.ContainsKey(key))
{
return base[key];
}
}
return default(V);
}
///
/// 当Key为int时,通过此方法取值
///
public void Set(K key, V value)
{
lock (lockObj)
{
if (base.ContainsKey(key))
{
base[key] = value;
}
else
{
base.Add(key, value);
}
}
}
public new void Clear()
{
if (Count > 0)
{
lock (lockObj)
{
if (Count > 0)
{
base.Clear();
}
}
}
}
public new bool ContainsKey(K key)
{
lock (lockObj)
{
return base.ContainsKey(key);
}
}
}
[Serializable]
public partial class MDictionary
{
protected MDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}