Shival
Shival

Reputation: 274

SignalR. Timer is not stopping on the server

We are using SignalR for information exchange.

When the web browser is connected a timer starts, but it is not stopping when user close the browser. Here is the code. starttimer function runs when browser connected. When user disconnect the browser, timer still running on the server.

[HubName("myChatHub")]
public class InboundCallsDataShare : Hub
{
    private OverrideTimer timer ;
    private List<GroupNConnectionId> groupsList = new List<GroupNConnectionId>();
    public void send(string message)
    {
        Clients.All.addMessage(message);
        //Clients..addMessage(message);

    }

    public void starttimer(string queue)
    {
        //var connectionId = this.Context.ConnectionId;
        //GroupNConnectionId objGroupNConnectionId=new GroupNConnectionId();
        //objGroupNConnectionId.Group = queue;
        //objGroupNConnectionId.ConnectionID = connectionId;
        //if(groupsList.Contains(objGroupNConnectionId))return;
        //////////////////////////////////////////////////////
        //groupsList.Add(objGroupNConnectionId);
        Groups.Add(this.Context.ConnectionId, queue);
        timer = new OverrideTimer(queue);
        timer.Interval = 15000;
        timer.Elapsed +=new EventHandler<BtElapsedEventArgs>(timer_Elapsed);
        //first time call
        timer_Elapsed(timer,new BtElapsedEventArgs(){Queue = queue});
        //ends
        timer.Start();
        Console.WriteLine("Timer for queue " +queue);
    }

    public override Task OnConnected()
    {
       return base.OnConnected();
    }
    public override Task OnDisconnected()
    {

        //timer.Stop();
        return base.OnDisconnected();
    }



    public void getdatafromxml(string queue)
    {

        string list = (new Random()).Next(1, 10000).ToString();
        Clients.All.getList(list);

        //Clients..addMessage(message);

    }
    public ICBMObject GetInterationList(string queue)
    {
        //ININInterations.QueueListViewItemData _obj = new ININInterations.QueueListViewItemData();
        return GetInboundCallCountFromXML(queue);
        //return _obj.MainFunctionIB();

    }

    void timer_Elapsed(object sender, BtElapsedEventArgs e)
    {

        ICBMObject objICBMObject = GetInboundCallCountFromXML(e.Queue);
        Clients.Group(e.Queue).getList(objICBMObject);
        CreateFile(e.Queue);
        //Clients.All.getList(objICBMObject);
    }

    private void CreateFile(string queue)
    {
        string path = @"D:\t.txt";
        string text = File.ReadAllText(path);
        text += queue+ DateTime.Now.ToString() + Environment.NewLine;
        File.WriteAllText(path, text);
    }
    public ICBMObject GetInboundCallCountFromXML(string queue)
    {
        FileStream fs = null;
        int totalInboundCalls = 0,
                totalInboundCallsUnassigned = 0;
        string longestDuration = "";
        bool updateText = false;
        try
        {
            XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNodeList xmlnode;
            int i = 0;
            string str = null;
            fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "InboundXML/" + queue + ".xml",
                FileMode.Open, FileAccess.Read);
            if (fs.CanRead)
            {
                xmldoc.Load(fs);
                xmlnode = xmldoc.GetElementsByTagName(queue);

                for (i = 0; i <= xmlnode.Count - 1; i++)
                {

                    totalInboundCalls = Convert.ToInt32(xmlnode[i].ChildNodes.Item(0).InnerText.Trim());
                    totalInboundCallsUnassigned = Convert.ToInt32(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
                    longestDuration = xmlnode[i].ChildNodes.Item(2).InnerText.Trim();

                }
                updateText = true;
            }
        }
        catch (Exception)
        {


        }
        finally
        {
            if (fs != null)
            {
                fs.Close();
                fs.Dispose();
            }
        }


        return new ICBMObject()
        {
            TotalInboundCalls = totalInboundCalls,
            TotalInboundCallsUnassigned = totalInboundCallsUnassigned,
            LongestDuration = longestDuration,
            UpdateText = updateText
            //string.Format("{0:D2}:{1:D2}:{2:D2}",
            //    _LongetInbound.Hours,
            //    _LongetInbound.Minutes,
            //    _LongetInbound.Seconds)
        };
    }

}

Upvotes: 1

Views: 213

Answers (1)

Kelso Sharp
Kelso Sharp

Reputation: 972

Besides the fact that its commented out? Did you put a break point on the timer to see if its getting hit at all? It might be that there is a delay in calling the onDisconnect, if the timeout property is set too large, it might not fire. it might be entering onReconnected if it does not know the client is closed.

Upvotes: 2

Related Questions