title: How can I declare many timers and stop a timer when a time had expired
body: <p>I'll make my description as detailed as possible. thanks in advance.</p>
  
  <p>Is there a way to declare many timers like using array or other methods? and stop the timer when timer expires or time has elapsed?</p>
  
  <p>I have tried t.Stop(); and other timer methods but i cant make it work.
  this code shows the message box every minute or whatever the user inputted in the timer. The problem is that i can seem to stop a timer when i have multiple timers.</p>
  
  <p>I have a form that allows the user to input the timer,for example 1 minute, this form pops up when i selected a row in the data grid view, the code converts the Hours or minutes into seconds and is working fine.</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
      {
          if (textBox1.Text == "")
          {
              MessageBox.Show("Please Indicate How Many Minutes/Hours.");
          }
          else
          {
              string desu = textBox1.Text;
              int temp = 0;
              if (int.TryParse(desu, out temp))
              {
                  if (comboBox1.Text == "Hours")
                  {
                      //hours to seconds
                      time = temp * 3600;
                      eta = desu + "Hours";
  
                      this.Close();
                  }
                  else if (comboBox1.Text == "Minutes")
                  {
                      //minutes to seconds
                      time = temp * 60;
                      eta = desu + "Minutes";
  
                      this.Close();
                  }
              }
              else
              {
                  MessageBox.Show("Please enter a valid integer");
              }
          }
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OOOil.png" rel="nofollow noreferrer">after you input the timer, after 1 minute a message box will appear and stop the current timer.</a></p>
  
  <p>here is the method to call when starting the timer</p>
  
  <pre><code> public void Start()
      {
  
  
          ETA_Input frm = new ETA_Input(this);
          startTime = DateTime.Now;
          t = new System.Timers.Timer(1000 * f);
          t.Elapsed += timer_Elapsed;
          t.Enabled = true;
  
      }
  </code></pre>
  
  <p>and then here is where it trigger the form where you can input hours or minutes, and pass the row value to the list view while changing one of the value in the datagridview and to the databse.</p>
  
  <pre><code>private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
      {
          ETA_Input frm = new ETA_Input(this);
          frm.ShowDialog();
          if (frm.time != 0)
          {
              string name = dataGridView1.CurrentRow.Cells[0].Value.ToString();
              int deku = Int32.Parse(name);
              string constring = "server=localhost;database=dbhorizon;uid=root;password=1234";
              string Query = "update tblUserIdd set User_Available = 'Ongoing' where User_ID='" + deku + "' ";
  
              MySqlConnection conDatabase = new MySqlConnection(constring);
              MySqlCommand cmdDatabase = new MySqlCommand(Query, conDatabase);
              MySqlDataReader myReader;
  
              conDatabase.Open();
              myReader = cmdDatabase.ExecuteReader();
              dgvref();
  
              string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
              string naem = dataGridView1.CurrentRow.Cells[1].Value.ToString();
              string field = dataGridView1.CurrentRow.Cells[2].Value.ToString();
  
              f = frm.time;
  
              Start();
  
  
  
              seconds = string.Format("{0:HH:mm:ss tt}", DateTime.Now);
  
  
              string[] row = { id, naem, field, seconds, frm.eta,custb  };
              var listViewItem = new ListViewItem(row);
              listView1.Items.Add(listViewItem);
  
  
          }
  
      }
  </code></pre>
  
  <p>and here is the time elapsed event where i intended to stop the timer that the time has expired and remove the item in the list view while changing a value in the datagridview to the database. i cant also seem to remove the item where the time has expired or elapsed.</p>
  
  <pre><code>void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          TimeSpan timeSinceStart = DateTime.Now - startTime;
          MessageBox.Show("A Service is finished");
  
          string name = dataGridView1.CurrentRow.Cells[0].Value.ToString();
          int deku = Int32.Parse(name);
          string constring = "server=localhost;database=dbhorizon;uid=root;password=1234";
          string Query = "update tblUserIdd set User_Available = 'Available' where User_ID='" + deku + "' ";
  
          MySqlConnection conDatabase = new MySqlConnection(constring);
          MySqlCommand cmdDatabase = new MySqlCommand(Query, conDatabase);
          MySqlDataReader myReader;
  
          conDatabase.Open();
          myReader = cmdDatabase.ExecuteReader();
          dgvref();
      }
  </code></pre>