Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have an array with 3 elements for example:
string[] message = {"how","are","you"};

For each of the element, I will assign a timer event with different duration. For example, "how" will display for one minute, "are" with 30 seconds and "you" with 2 minutes. I have successfully added timer for only one element which is "you" as I am using for loop to iterate over each of them but realizing this is not the correct method as the previous two elements' duration will get overwritten with the last duration which is 2 minutes.
Besides, if any of the timer ends, for example "are" will end first as it only has 30 seconds of duration, then system will fire the other 2 elements to my users which are "how" and "you" respectively and again only "you" after "how" is expired.
I am clueless on how should I proceed with my functionality. Below are my current codes:

public string NotifyAllClients(string application, string region, string[] message)
{
   if (message.Count() > 0)
   {
       //begin countdown of duration using timer class
       CountDownTimer(message);
       //send message to users
       Clients.Group(application).NotifyAllClients(region, message);
    }
    return "Success!!";
}



public void CountDownTimer(string[] messageWithDuration)
{
     //loop through the array to assign each elements with duration
     foreach (string msg in messageWithDuration)
     {
         notificationList.ModelList.Add(new NotificationModel()
         {
             Message = msg,
             DisplayFlag = true
         });

         //hardcoded a value here for testing, clueless on how to assign different 
         //value
         counterTimerValue = 120; //2 minutes in seconds

         displayTimer.Interval = 1000; // 1 second
         displayTimer.Elapsed += new ElapsedEventHandler(timerTick);
         displayTimer.Enabled = true;
         displayTimer.Start();
      }
}



private void timerTick(Object source, EventArgs e)
{
     counterTimerValue--;
     if (counterTimerValue == 0)
     {
         displayTimer.Stop();
         try
         {
             List<string> list = new List<string>();
             //hardcoded the first element for flag false to filter out itself
             //should have according to timer which has reach 0
             notificationList.ModelList[0].DisplayFlag = false;
             //only resend the message with flag as true
             notificationList.ModelList = notificationList.ModelList.Where(x => x.DisplayFlag == true).ToList();
             foreach(NotificationModel model in notificationList.ModelList)
             {
                 list.Add(model.Message);
             }
             messageContent = list.ToArray();

             Clients.Group(application).NotifyAllClients(region, message);
         }
         catch (Exception)
         {
         }
    }
}



I have comment out some codes to simplify the question posted.

What I have tried:

1. Search online for ideas/brainstorming but no avail.
Posted
Comments
Graeme_Grant 26-Sep-17 3:56am    
Multiple timers of this nature is very inefficient.

You would be better off with a single timer that checks a list of objects with a NotifyTime DateTime property every second. Then you simple check the current time against the NotifyTime and trigger those that require triggering.
Jamie888 26-Sep-17 4:02am    
Sir, do you mean getting those 3 different durations and set a single timer to check against each of them every second and trigger the required logic once the times up for any of them?
Graeme_Grant 26-Sep-17 5:57am    
timer interval is subjective to the rounding of the trigger time. But yes, on the tick event, only trigger/process/etc those that qualify.

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