Click here to Skip to main content
15,444,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to make more than one count down timers. I use signalr for send my count down timer value to client. When I create one timer, its normal but when the second timer or more than one timers, the tick for count down run very fast.
Please help me.

What I have tried:

My create timer function:
public void Start(int id, int tick,int warningtime)
       {
           var user = users.FirstOrDefault(x=>x.ID==id);

           if(user==null)
           {
               UserGroups cluser = new UserGroups();
               if(users.Count>=1)
               {
                   multiple += 1;
               }
               else
               {
                   multiple = 1;
               }
               cluser.ID = id;
               cluser.counter = tick;
               cluser.timers = new Timer();
               if(multiple>1)
               {
                   cluser.timers.Interval = 1000 * (multiple*10*1.5);
               }
               else
               {
                   cluser.timers.Interval = 1000;
               }
               cluser.timers.Elapsed += TimerTick;
               cluser.warnTime = warningtime;
               users.Add(cluser);
               cluser.timers.Start();
           }
           else
           {
               //activated the exist timer
               if (user.counter == 0 && user.warnTime == 0 && user.ID == id)
               {
                   user.counter = tick;
                   user.timers = new Timer();
                   if (multiple > 1)
                   {
                       user.timers.Interval = 1000 * (multiple * 10 * 1.5);
                   }
                   else
                   {
                       user.timers.Interval = 1000;
                   }
                   user.timers.Elapsed += TimerTick;
                   user.warnTime = warningtime;
                   user.timers.Start();
               }
           }
       }


My Tick Timer:
private void TimerTick(object sender, ElapsedEventArgs e)
{
    List<object> result = new List<object>();
    var context = GlobalHost.ConnectionManager.GetHubContext<TimerHub>();

    foreach(var item in users)
    {
        // kill timer automatically after 1 hour
        if(item.counter==-3600)
        {
            TimerHub Obj = new TimerHub();
            Obj.Stop(item.ID);
        }
        //broadcast just the active timer
        if(item.warnTime!=0)
        {
            item.counter--;
            bool warning = false;
            //set the timer item
            string timerResult = CalculateCountTimer(item.counter);

            if (item.counter <= item.warnTime)
            {
                warning = true;
            }
            else
            {
                warning = false;
            }
            //item for send to client
            result.Add(new
            {
                scn = item.ID,
                warning = warning,
                resultTime = timerResult,
                reset = false
            });
        }
    }
    context.Clients.All.countdown(result);

}
Posted
Comments
Gerry Schmitz 7-May-18 14:50pm    
You got some "weird" timer interval calculations going on there; that's your problem.
Andy07 7-May-18 22:07pm    
The calculation is made because the timers were going to fast when it create more than one timer so I made something in the interval calculation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900