【发布时间】:2014-06-02 06:29:54
【问题描述】:
我正在使用 SignalR 从数据库中为每个连接的客户端获取实时更新。每个客户端都有唯一的数据,所以我不能只运行一个实时更新实例。我为每个客户创建一个新对象。问题是对象有 System.Threading.Timer 每秒运行回调以从数据库获取更新。即使在客户端断开连接后,计时器也会继续运行。在断开连接事件中我无权访问对象。我该如何阻止它?
public class DataHub : Hub
{
private readonly RealTimeData data;
public DataHub(RealTimeData rdata)
{
data = rdata;
}
public void Start(Int64 routerId)
{
data.StartTimer(routerId);
}
}
public class RealTimeData
{
private IHubConnectionContext Clients;
public Timer timer;
private readonly int updateInterval = 1000;
private readonly object updateRecievedDataLock = new object();
private bool updateReceivedData = false;
List<Items> allItems = new List<Items>();
public void StartTimer(Int64 routerId)
{
this.routerId = routerId;
timer = new Timer(GetDataForAllItems, null, updateInterval, updateInterval);
}
public void GetDataForAllItems(object state)
{
if (updateReceivedData)
{
return;
}
lock (updateRecievedDataLock)
{
if (!updateReceivedData)
{
updateReceivedData = true;
//get data from database
allItems = Mapper.Instance.GetDataForAllItems(routerId);
updateReceivedData = false;
//send it to the browser for update
BroadcastData(allItems);
}
}
}
}
public override Task OnDisconnected()
{
//before ondisconnect is called datahub construtor is called and a new instace of real time data is made. So I can't have access to previous object here. Where do I stop the timer?
}
【问题讨论】:
-
我注意到您只有一个
DataHub构造函数,并且它需要一个RealTimeData对象作为参数。 Hub是如何被实例化的?你用 SignalR 的依赖解析器注册了吗? -
是的,我在启动时添加了依赖解析器
-
你有解决方案吗?