如何在c#中为特定值分配特定的计时器EventHandler

发布于 2024-12-09 12:41:39 字数 2747 浏览 0 评论 0原文

我有计时器数组。我希望计时器0与_dataValues中的第一个数据值一起工作,第二个计时器与_dataValues中的第二个数据值一起工作,第三个计时器与_dataValues中的第三个数据值一起工作,等等,并在窗口形式的标签中显示结果。我使用System.Windows .Forms.Timer。这是我的代码,

void PrepareTimers(List<int> _dataValues)    
{
    int i = 0;

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

        t[i] = dataValue.ToString();
        a[i] = timer1[i].ToString();
        a[i] = t[i];

        timer1[i].Tick += new EventHandler(timer_Tick);
        timer1[i].Interval = (1000) * (1);                   

        label = new Label();
        label.Name = "label_name" + i;
        label.Size = new Size(200, 20);
        label.Location = new Point(45, 100 + i * 30);
        label.TabIndex = i;
        label.Visible = true;
        this.Controls.Add(label);                    

        dict[timer1[i]] = label;
        timer1[i].Enabled = true;
        timer1[i].Start();
        i++;    
    }
}

private void timer_Tick =(object sender, EventArgs e)    
{       
    string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
    MySqlConnection mycon = new MySqlConnection(myconstring);

    i = 0;
    string u = "UPDATED";
    mycon.Open();

    foreach (int dataValue in _dataValues)               
    {                
        if (a[i] == dataValue.ToString())
        {
            MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag =  " + (dataValue.ToString()) + " *2 , sendingstatus = '" + u + "' WHERE flag = " + dataValue.ToString() + " LIMIT 1", mycon);

            cmd.ExecuteNonQuery();
            Console.WriteLine("row update" + dataValue.ToString());          

            mycon.Close();

            mycon.Open();
            string sql = "SELECT count(flag) FROM sms_data_bankasia where sendingstatus='UPDATED' AND flag = " + (dataValue.ToString()) + " *2 group by flag";
            MySqlCommand comd = mycon.CreateCommand();
            comd.CommandText = sql;
            MySqlDataReader dtr = comd.ExecuteReader();
            try
            {
                while (dtr.Read())
                {
                    Timer t = (Timer)sender;
                    dict[t].Text = dtr[0].ToString() + " program Updated " + dataValue.ToString();                                
                }
                dtr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        i++;
    }
    mycon.Close();
}

但它每秒更新 4 行,每个 EventHandler 都处理每个 dataValue。我想要第一个计时器更新1 秒内 1 行,并在标签中显示 1 行用第一个数据值更新,第二个计时器在 1 秒内更新 1 行,并在另一个标签中显示 1 行用第二个数据值更新,依此类推。我应该做什么?任何人都可以帮助我吗?

I have array of timer.I want timer0 work with 1st dataValue in _dataValues, 2nd timer work with 2nd dataValue in _dataValues, 3rd timer work with 3rd dataValue in _dataValues and etc and also show result in label in windows form.I use System.Windows.Forms.Timer.Here is my code,

void PrepareTimers(List<int> _dataValues)    
{
    int i = 0;

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

        t[i] = dataValue.ToString();
        a[i] = timer1[i].ToString();
        a[i] = t[i];

        timer1[i].Tick += new EventHandler(timer_Tick);
        timer1[i].Interval = (1000) * (1);                   

        label = new Label();
        label.Name = "label_name" + i;
        label.Size = new Size(200, 20);
        label.Location = new Point(45, 100 + i * 30);
        label.TabIndex = i;
        label.Visible = true;
        this.Controls.Add(label);                    

        dict[timer1[i]] = label;
        timer1[i].Enabled = true;
        timer1[i].Start();
        i++;    
    }
}

private void timer_Tick =(object sender, EventArgs e)    
{       
    string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
    MySqlConnection mycon = new MySqlConnection(myconstring);

    i = 0;
    string u = "UPDATED";
    mycon.Open();

    foreach (int dataValue in _dataValues)               
    {                
        if (a[i] == dataValue.ToString())
        {
            MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag =  " + (dataValue.ToString()) + " *2 , sendingstatus = '" + u + "' WHERE flag = " + dataValue.ToString() + " LIMIT 1", mycon);

            cmd.ExecuteNonQuery();
            Console.WriteLine("row update" + dataValue.ToString());          

            mycon.Close();

            mycon.Open();
            string sql = "SELECT count(flag) FROM sms_data_bankasia where sendingstatus='UPDATED' AND flag = " + (dataValue.ToString()) + " *2 group by flag";
            MySqlCommand comd = mycon.CreateCommand();
            comd.CommandText = sql;
            MySqlDataReader dtr = comd.ExecuteReader();
            try
            {
                while (dtr.Read())
                {
                    Timer t = (Timer)sender;
                    dict[t].Text = dtr[0].ToString() + " program Updated " + dataValue.ToString();                                
                }
                dtr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        i++;
    }
    mycon.Close();
}

But it update 4 row in per sec and every EventHandler work with every dataValue.I want 1st timer update 1 row in 1 sec and show in label that 1row update with 1st dataValue and 2nd timer update 1row in 1 sec and show in another label that 1 row update with 2nd dataValue and so on.What should i do? Any one can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-12-16 12:41:39

PrepareTimers 中为每个计时器分配一个标签:timer1[i] = new Timer();定时器1[i].Tag = i;
然后您可以在timer_Tick中使用它,而不是循环整个数组:

i = (int)((Timer)sender).Tag;
int dataValue = _dataValues[i];
//...

而不是:

i = 0;
foreach (int dataValue in _dataValues)
{
    if (a[i] == dataValue.ToString())
    //...

In PrepareTimers assign a tag to each timer: timer1[i] = new Timer(); timer1[i].Tag = i;
Then you can use this in timer_Tick instead of looping through the whole array:

i = (int)((Timer)sender).Tag;
int dataValue = _dataValues[i];
//...

instead of:

i = 0;
foreach (int dataValue in _dataValues)
{
    if (a[i] == dataValue.ToString())
    //...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文