IsolatedStorageSettings线程安全调用方式
来源:互联网 发布:随州seo服务 编辑:程序博客网 时间:2024/05/07 12:11
提供一个线程安全的访问IsolatedStorageSettings的方式。
/// <summary>/// IsolatedStorageSettings是非线程安全的,多线程环境下对临界资源的访问应该互斥进行/// 警告,对于IsolatedStorageSettings的访问都应该通过本类,严禁直接访问。/// </summary>using System;using System.IO.IsolatedStorage;public class ApplicationSettingHelper{ private static object _sync = new object(); /// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static bool AddEntity(string key, object value) { bool isAdd = false; try{ if (!string.IsNullOrEmpty(key) && value != null) { lock (_sync) { if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == false){ IsolatedStorageSettings.ApplicationSettings.Add(key, value); IsolatedStorageSettings.ApplicationSettings.Save(); isAdd = true; } else { IsolatedStorageSettings.ApplicationSettings[key] = value; IsolatedStorageSettings.ApplicationSettings.Save(); isAdd = true; } } } } catch (Exception exception) { //LoggingHelper.Log("AddEntity", exception, true); throw exception; } return isAdd; } /// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public static string GetEntityString(string key) { string strCacheEntiy = string.Empty; try { if (!string.IsNullOrEmpty(key)) { lock (_sync) { if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true) { strCacheEntiy = IsolatedStorageSettings.ApplicationSettings[key].ToString(); } } } } catch (Exception exception) { //LoggingHelper.Log("GetEntityString", exception,true); throw exception; } return strCacheEntiy; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static T GetEntityObj<T>(string key) { T objCacheEntity; try { if (!string.IsNullOrEmpty(key)) { lock (_sync) { if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true) { var settings = IsolatedStorageSettings.ApplicationSettings;settings.TryGetValue(key, out objCacheEntity);return objCacheEntity;} } } } catch (Exception exception) { //LoggingHelper.Log("GetEntityObj", exception,true); throw exception; } return default(T); } public static bool RemoveEntity(string key) { bool isRemove = false; try { if (!string.IsNullOrEmpty(key)) { lock (_sync) { if (IsolatedStorageSettings.ApplicationSettings.Contains(key) == true){ IsolatedStorageSettings.ApplicationSettings.Remove(key); IsolatedStorageSettings.ApplicationSettings.Save(); isRemove = true; } } } } catch (Exception exception) { //LoggingHelper.Log("RemoveEntity", exception,true);throw exception; } return isRemove; } /// <summary> /// /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool IsIsolateStorageKeyExists(string key) { bool isExist = false; try { if (!string.IsNullOrEmpty(key)) { lock (_sync) { isExist = IsolatedStorageSettings.ApplicationSettings.Contains(key); } } } catch (Exception exception) { //LoggingHelper.Log("IsIsolateStorageKeyExists", exception, true); throw exception; } return isExist; }}
- IsolatedStorageSettings线程安全调用方式
- thrift客户端调用不支持多线程方式,非线程安全
- Java线程安全-同步方式
- Windows Form线程安全调用
- 线程通信,线程安全及解决方式
- C#线程函数调用方式
- 使用多线程提高 Windows 窗体应用程序的性能时,必须注意以线程安全方式调用控件
- servlet的线程安全处理方式
- UI多线程、开辟方式及线程安全
- share_ptr的线程安全读写方式
- Java多线程线程安全实现方式
- java 线程安全的实现方式
- 线程安全的延迟初始化方式
- 哪些方式实现可以线程安全
- 实现线程安全的几种方式
- Windows 窗体控件进行线程安全调用
- Vs2005 安全的线程调用举例
- 跨线程安全调用Windows 窗体控件
- 反射 类的构造函数
- Android 5种方式存储数据
- 一份非常内行的Linux LVM HOWTO
- libgdx学习笔记02
- mvc下是如何传值的
- IsolatedStorageSettings线程安全调用方式
- 《圈圈教你玩USB》之 USB鼠标
- QML FocusScope的问题
- JUnit4概述
- opensips源码分析
- 李笑来---如何选书
- C++调用java的方法
- DOJO dijit.dialog
- CKeditor编辑器的配置