0

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();
    }
yuzirui
  • 51
  • 10

2 Answers2

0
        int numberOfThreads = 0;
        int seconds = 0;
        List<System.Threading.Thread> timer = new List<System.Threading.Thread>();//(timex(6))>();

        //create many threads together
        for (int i = 0; i < numberOfThreads; i++)
        {
            System.Threading.Thread tempx = new System.Threading.Thread(timex(seconds));
            timer.Add(tempx);
            tempex = null;
            timer[i].Start();
        }

        //each time a timer is needed
        System.Threading.Thread temp = new System.Threading.Thread(timex(seconds));
        temp.Start();


        private System.Threading.ThreadStart timex(int seconds)
        {
            for (int i = 0; i < seconds; i++)
                {
                    //your code here
                    System.Threading.Thread.Sleep(1000);
                }
            MessageBox.Show("A service has finished");
            return null;
        }
Afnan Makhdoom
  • 654
  • 1
  • 8
  • 20
  • I cant seem to make this work the way i wanted to. it fires the messagebox yes but every 1 sec that is. what im trying to do is end a timer using timer elapsed but resuming or without stopping the other timer at the other value. – yuzirui Aug 26 '17 at 09:40
  • You have multiple threads, give each thread the value you want, the period span you want, the input you want, make them run spontaneous or however you want – Afnan Makhdoom Aug 26 '17 at 15:13
  • by the way sir, i have posted my answer. though other problem have appeared. – yuzirui Aug 26 '17 at 15:37
0

i think im close enough to what i want. similar to what i want

timer2.Start();

I put the code above below the InitializeComponent();

an then

private DateTime startTime;

I declared a date time somewhere, sorry my code is messy.

and then I grab the datetime here instead at Start();

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;




            startTime = DateTime.Now;

            secondss = string.Format("{0:HH:mm:ss tt}", DateTime.Now);


            string[] row = { id, naem, field, secondss, frm.eta,custb, f.ToString()  };
            var listViewItem = new ListViewItem(row);
            listView1.Items.Add(listViewItem);


        }

    }

and then i changed my elapsed event into tick event and here

private void timer2_Tick(object sender, EventArgs e)
    {

        foreach (ListViewItem items in listView1.Items)
        {
            TimeSpan timeSinceStart = DateTime.Now - startTime;
            items.SubItems[6].Text = string.Format("{0}h {1}m {2}s", timeSinceStart.Hours, timeSinceStart.Minutes, timeSinceStart.Seconds);

        }

The timer is working though the Message Box doesn't appear yet. also the timer counts up, either way its not a problem as long as it works.

but theres another problem, everytime i input a time, the previous counters/countdowns resets to 0. here is the image where it resets to 0

yuzirui
  • 51
  • 10