Search code examples
c#arraystimer

How to gate array of timer in C#


I want get a timer foreach dataValue in _dataValues which are stored in array for example, timer[0]=timer0, timer[1]=timer1, timer[2]=timer2 etc. Here is my code:

Timer[] timer = new Timer[50];

int i = 0;

foreach (int dataValue in _dataValues)
{             
     string a = "timer" + i;              
     timer[i] = a;
     i++;
     timer1[i].Tick += new EventHandler(timer_Tick);
     timer1[i].Interval = (1000) * (2);
 }

But it provide error "Cannot implicitly convert type 'string' to 'System.Windows.Forms.Timer'". So how to get value in timer[i]? Any one can help me?


Solution

  • It is not really clear what you are after. Here is a wild guess:

    Timer[] timers = new Timer[50];
    string timerNames = new string[50];
    
    int i = 0;
    
    foreach (int dataValue in _dataValues)
    {    
         timeNames[i] = string.Format("{0}",i);
         timer[i] = new Timer();
         timer[i].Interval = dataValue;
         i++;
    }
    

    It is probably a bad idea to create 50 timers. If you want to have different intervals this can be done with a single timer too. Just create one timer with the shortest common interval.