C# 如何在信号器中的OnDisconnected事件中停止计时器
当客户端请求在C# 如何在信号器中的OnDisconnected事件中停止计时器,c#,asp.net,timer,signalr,C#,Asp.net,Timer,Signalr,当客户端请求在PushNotificationData方法中建立集线器连接时,我启动了计时器 按照计时器间隔,它确实从数据库获取记录并推送到客户端。 但是,当客户端断开连接时,必须停止该计时器,而不是持续拉动 所以我使用了OnDisconnected事件来停止计时器。但不幸的是,计时器没有停止 这是我的密码: public class NotifyHub : Hub { private string ConnectionId; private int UserId; pr
PushNotificationData
方法中建立集线器连接时,我启动了计时器
按照计时器间隔,它确实从数据库获取记录并推送到客户端。
但是,当客户端断开连接时,必须停止该计时器,而不是持续拉动
所以我使用了OnDisconnected事件来停止计时器。但不幸的是,计时器没有停止
这是我的密码:
public class NotifyHub : Hub
{
private string ConnectionId;
private int UserId;
private int UserTypeId;
Timer timer = new Timer();
public override Task OnConnected()
{
ConnectionId = Context.ConnectionId;
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled)
{
timer.Stop();
timer.Enabled = false;
//logic code removed for brevity
return base.OnDisconnected(stopCalled);
}
public void PushNotificationData(Int32 userId, Int16 userTypeId)
{
UserId = userId;
UserTypeId = userTypeId;
ConnectionId = Context.ConnectionId;
timer.Elapsed += Timer_Elapsed1;
timer.Interval = 6000;
timer.Enabled = true;
timer.Start();
}
private void Timer_Elapsed1(object sender, ElapsedEventArgs e)
{
var notificationParams = new PushNotificationRequest
{
FilterProperty = new Common.Filters.FilterProperty { Offset = 0, RecordLimit = 0, OrderBy = "datechecked desc" },
Filters = new List<Common.Filters.FilterObject> { new Common.Filters.FilterObject { LogicOperator = 0, ConditionOperator = 0, Function = 0, FieldName = "", FieldValue = "", FieldType = 0 } }
};
using (INotificationManager iNotifity = new NotificationManager())
{
var taskTimer = Task.Run(async () =>
{
var NotificationResult = iNotifity.GetPushNotificationData(notificationParams, UserId, UserTypeId);
//Sending the response data to all the clients based on ConnectionId through the client method NotificationToClient()
Clients.Client(ConnectionId).NotificationToClient(NotificationResult);
//Delaying by 6 seconds.
await Task.Delay(1000);
//}
});
}
}
}
公共类NotifyHub:Hub
{
私有字符串连接ID;
私有int用户id;
私有int UserTypeId;
定时器=新定时器();
已连接的公用覆盖任务()
{
ConnectionId=Context.ConnectionId;
返回base.OnConnected();
}
公共覆盖任务OnDisconnected(bool stopCalled)
{
timer.Stop();
timer.Enabled=false;
//为简洁起见,删除了逻辑代码
返回基.OnDisconnected(stopCalled);
}
public void PushNotificationData(Int32用户ID、Int16用户类型ID)
{
UserId=UserId;
UserTypeId=UserTypeId;
ConnectionId=Context.ConnectionId;
timer.appeased+=计时器延迟1;
定时器间隔=6000;
timer.Enabled=true;
timer.Start();
}
专用无效计时器\u Elapsed1(对象发送方,ElapsedEventArgs e)
{
var notificationParams=新的PushNotificationRequest
{
FilterProperty=new Common.Filters.FilterProperty{Offset=0,RecordLimit=0,OrderBy=“datechecked desc”},
过滤器=新列表{new Common.Filters.FilterObject{LogicOperator=0,ConditionOperator=0,Function=0,FieldName=”,FieldValue=”,FieldType=0}}
};
使用(INotificationManager iNotifity=new NotificationManager())
{
var taskTimer=Task.Run(异步()=>
{
var NotificationResult=iNotifity.GetPushNotificationData(notificationParams,UserId,UserTypeId);
//通过客户端方法NotificationToClient()将响应数据基于ConnectionId发送给所有客户端
Clients.Client(ConnectionId)、NotificationToClient(NotificationResult);
//延迟6秒。
等待任务。延迟(1000);
//}
});
}
}
}
当我调试它时,它会显示timerenabled=true
,即使在OnDisconnected
触发之后也是如此
当断开连接的
ondisconnected
正在执行时,我可以看到计时器得到更新enabled=false
。当它从断开的计时器中出来后,启用的再次获得true
。阅读有关集线器对象生存期的信息。重要的是这一点
因为Hub类的实例是暂时的,所以不能使用它们来维护从一个方法调用到下一个方法调用的状态。每次服务器接收到来自客户端的方法调用时,Hub类的新实例都会处理该消息。要通过多个连接和方法调用来维护状态,请使用其他一些方法,例如数据库、Hub类上的静态变量或不是从Hub派生的其他类。如果使用Hub类上的静态变量等方法将数据持久化到内存中,则当应用程序域回收时,数据将丢失
实际上,每次创建一个新的集线器时,都要创建一个新的计时器。因此,您使用了多个计时器,所有这些计时器都调用Timer_Elapsed1方法。您可以尝试将计时器设为静态,并跟踪连接计数。这样,当所有客户端都断开连接时,您可以停止计时器。请注意,如果应用程序域进行回收(如上文档所述),即使是静态变量也可能丢失。不能为每个连接创建计时器吗?因此,一旦用户断开连接,我们就可以停止计时器。A你需要重构这个设计。集线器并不意味着保留状态。