I want to stop System.Timers.Timerwhich is running in the SignalR hub after the client closes the window / tab containing the active connection.
I tried sending the bool value to the server by calling the server code to notify the server to which the client is still connected or not, but it is currently not working.
window.onbeforeunload = function () {
profile.server.setIsConnected(false);
};
Server side:
public ProfileHub()
{
timer = new Timer(15000);
timer.Elapsed += (sender, e) => { timer_Elapsed(sender, e, _isActive); };
timer.AutoReset = false;
}
[Authorize]
private void timer_Elapsed(object sender, ElapsedEventArgs e, bool active)
{
timer.Stop();
if (active)
{
System.Diagnostics.Debug.WriteLine("Timer Started");
timer.Start();
}
else
{
System.Diagnostics.Debug.WriteLine("Timer Stopped");
return;
}
}
[Authorize]
public void SetIsActive(bool isActive)
{
_isActive = isActive;
}
Is this possible, and am I on the right track? I suspect this has something to do with the anonymous delegate for timer.Elapsed, but I'm not quite sure.
source
share