I'll make my description as detailed as possible. thanks in advance.
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?
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.
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.
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");
}
}
}
after you input the timer, after 1 minute a message box will appear and stop the current timer.
here is the method to call when starting the timer
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;
}
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.
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);
}
}
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.
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();
}