开发者

How can assign specific timer EventHandler for specific value in c#

开发者 https://www.devze.com 2023-04-12 22:53 出处:网络
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 res

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(Li开发者_运维问答st<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?


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())
    //...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号