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

时间:2011-10-12 07:10:55

标签: c# .net winforms timer

我有计时器数组。我希望timer0与_dataValues中的第一个dataValue一起工作,第二个计时器与_dataValues中的第二个dataValue一起工作,第三个计时器与_dataValues中的第三个dataValue一起工作,并且还以windows形式显示标签中的结果。我使用System.Windows.Forms.Timer.Here是我的代码,

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行,并在标签中显示1row update with 1st dataValue和2nd timer update 1row in 1 sec并显示另一个标签,1行用第二个dataValue更新等等。我该怎么办?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

PrepareTimers为每个计时器分配一个标记:timer1[i] = new Timer(); timer1[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())
    //...