c# - 如何声明多个计时器并在时间到期时停止计时器

标签 c# mysql listview datagridview timer

我会尽可能详细地描述我的描述。提前致谢。

有没有办法像使用数组或其他方法一样声明多个计时器?当计时器到期或时间已过时停止计时器?

我尝试过 t.Stop();和其他计时器方法,但我无法使其工作。 此代码每分钟或用户在计时器中输入的任何内容显示消息框。问题是,当我有多个计时器时,我似乎可以停止计时器。

我有一个表单,允许用户输入计时器,例如 1 分钟,当我在数据 GridView 中选择一行时会弹出此表单,代码将小时或分钟转换为秒并且工作正常。

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.

这里是启动定时器时调用的方法

 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;

    }

然后在这里触发表单,您可以在其中输入小时或分钟,并将行值传递到 ListView ,同时更改 datagridview 和数据库中的值之一。

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);


        }

    }

这是耗时的事件,我打算停止时间已过期的计时器,并删除 ListView 中的项目,同时将 datagridview 中的值更改为数据库。我似乎也无法删除时间已过期或已过的项目。

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();
    }

最佳答案

我认为我已经足够接近我想要的了。与我想要的类似

timer2.Start();

我将上面的代码放在InitializeComponent()下面;

然后

private DateTime startTime;

我在某处声明了日期时间,抱歉我的代码很困惑。

然后我在 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);


        }

    }

然后我将经过的事件更改为刻度事件,并在此处

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);

        }

计时器正在工作,但消息框尚未出现。计时器也会计时,无论哪种方式,只要它有效就不是问题。

但是还有一个问题,每次我输入一个时间,之前的计数器/倒计时都会重置为0。 here is the image where it resets to 0

关于c# - 如何声明多个计时器并在时间到期时停止计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45881422/

相关文章:

mysql - 我的.cnf : Improve speed of adding new columns and indexes in large MySQL tables

c# - 我需要一个最佳算法来找到数字 N 的最大除数。最好是在 C++ 或 C# 中

mysql - 为什么两个不同的 mysql 函数在对从单个 mysql_query() 检索的数据执行时返回不同的行?

c# - 多个 Web 服务中的相同对象导致重复的类

mysql - 从 SQL 中的多个表中选择 'field_name'?

java - 带有 ListView 和按钮的 Android 布局

qt - QML:同步滚动多个ListView

python - Kivy代码显示ListView崩溃

c# - .net 4.0 中的 System.Web.UI.DataVisualization.Charting 丢失错误?

c# - .NET 4 缓存支持