using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace KevDrawServer { public class GlobalSession { /// /// 存储共享数据的全局字典 /// private static readonly Dictionary sharedData = new Dictionary(); /// /// Stores the data. /// /// The key. /// The data. public static void StoreData(string key, object data) { lock (sharedData) { sharedData[key] = data; } } /// /// Gets the data. /// /// The key. /// An object. public static object GetData(string key) { lock (sharedData) { return sharedData.TryGetValue(key, out object data) ? data : null; } } public static bool ContainsKey(string key) { return sharedData.ContainsKey(key); } /// /// Tries the get value. /// /// The key. /// The value. /// A bool. public static bool TryGetValue(string key, out object value) { lock (sharedData) { return sharedData.TryGetValue(key, out value); } } /// /// 删除数据. /// /// The key. /// The data. /// A bool. public static bool TryRemove(string key, out object data) { lock (sharedData) { bool bFound = sharedData.TryGetValue(key, out data); if (bFound) { sharedData.Remove(key); return true; } } return false; } } }