bug
This commit is contained in:
57
ProductionMgr/Tnb.ProductionMgr/Helpers/TimerPoolHelper.cs
Normal file
57
ProductionMgr/Tnb.ProductionMgr/Helpers/TimerPoolHelper.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using JNPF.DependencyInjection;
|
||||
|
||||
namespace Tnb.ProductionMgr.Helpers
|
||||
{
|
||||
public class TimerPoolHelper : ISingleton
|
||||
{
|
||||
private static TimerPoolHelper instance = null;
|
||||
|
||||
public static TimerPoolHelper GetInstance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new TimerPoolHelper();
|
||||
}
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, Timer> _timers = new Dictionary<string, Timer>();
|
||||
private readonly object _lockObject = new object();
|
||||
|
||||
public TimerPoolHelper()
|
||||
{
|
||||
// 初始化代码,如果有必要的话
|
||||
}
|
||||
|
||||
public void StartTimer(TimerCallback timerCallback,object data,TimeSpan dueTime, TimeSpan period)
|
||||
{
|
||||
Timer timer;
|
||||
lock (_lockObject)
|
||||
{
|
||||
string timestamp = DateTime.Now.Ticks.ToString();
|
||||
Dictionary<string, object> dic = new Dictionary<string, object>();
|
||||
dic.Add("key",timestamp);
|
||||
dic.Add("value",data);
|
||||
timerCallback += DisposeTimer;
|
||||
timer = new Timer(timerCallback, dic, dueTime, period);
|
||||
_timers.Add(timestamp,timer);
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeTimer(object args)
|
||||
{
|
||||
Dictionary<string, object> dic = (Dictionary<string, object>)args;
|
||||
string key = dic["key"].ToString();
|
||||
if (_timers.ContainsKey(key))
|
||||
{
|
||||
_timers[dic["key"].ToString()].Dispose();
|
||||
_timers[dic["key"].ToString()] = null;
|
||||
_timers.Remove(dic["key"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user