62,046
社区成员
发帖
与我相关
我的任务
分享
public class DisposeableSingleton : IDisposable
{
private int innerData = 0;
//私有构造函数
private DisposeableSingleton(int n) { }
//析构函数,避免最终都没有执行Dispose
~DisposeableSingleton() { this.Dispose(); }
//做你释放工作,注意用变量区分是否释放过避免重复释放
public void Dispose()
{
_instance = null;//复位静态对象,下次再用重新申请
}
private static DisposeableSingleton _instance = null;
//属性只负责返回对象不检查是否为null
public static DisposeableSingleton Instance
{
get
{
return _instance;
}
}
//使用参数构造对象
public void CreateInstance(int i)
{
_instance = _instance == null ? new DisposeableSingleton(i) : _instance;
}
}
public class WorkflowRuntimeManager
{
/// <summary>
/// 单例模式的实例
/// </summary>
private static WorkflowRuntimeManager _instance = null;
public WorkflowRuntime wfRuntime { get; protected set; }
///// <summary>
///// 装饰器
///// </summary>
//private WorkflowRuntime _wfRuntime = null;
/// <summary>
/// 私有的构造函数
/// </summary>
protected WorkflowRuntimeManager()
{
//创建实例
wfRuntime = new WorkflowRuntime();
//添加核心服务
ExternalDataExchangeService exchangeService = new ExternalDataExchangeService();
wfRuntime.AddService(exchangeService);
}
/// <summary>
/// 返回WorkflowRuntimeManager的单例实例,如果实例不存在,创建实例
/// </summary>
/// <returns></returns>
public static WorkflowRuntimeManager getInstance()
{
if (_instance == null)
{
_instance = new WorkflowRuntimeManager();
}
return _instance;
}
public void AddSqlPersistenceService(string connectionString)
{
if (wfRuntime.IsStarted)
{
throw new ApplicationException("工作流运行时已经启动,无法添加核心服务!请在运行时启动前运行此方法!");
}
else
{
WorkflowPersistenceService persistenceService = new SqlWorkflowPersistenceService(connectionString);
wfRuntime.AddService(persistenceService);
}
}
public void StartWfRuntime()
{
if (!wfRuntime.IsStarted)
{
wfRuntime.StartRuntime();
}
}
public void StopWfRuntime()
{
if (wfRuntime.IsStarted)
{
wfRuntime.StopRuntime();
}
}
public void DestroyInstance()
{
if (_instance != null)
{
StopWfRuntime();
wfRuntime.Dispose();
_instance = null;
}
}
}
public class DisposeableSingleton : IDisposable
{
private int innerData = 0;
//私有构造函数
private DisposeableSingleton(int n) { }
//析构函数,避免最终都没有执行Dispose
~DisposeableSingleton() { this.Dispose(); }
//做你释放工作,注意用变量区分是否释放过避免重复释放
public void Dispose()
{
_instance = null;//复位静态对象,下次再用重新申请
}
private static DisposeableSingleton _instance = null;
//属性只负责返回对象不检查是否为null
public static DisposeableSingleton Instance
{
get
{
return _instance;
}
}
//使用参数构造对象
public void CreateInstance(int i)
{
_instance = _instance == null ? new DisposeableSingleton(i) : _instance;
}
}