1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

add items in listview using System.Threading.Timer

Discussion in 'C#' started by shopno, Nov 2, 2011.

  1. shopno

    shopno New Member

    Joined:
    Nov 2, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I want to add items in listview using threading timer that add items in listview in every 2 sec and this items come from MYsql db.I know that i should use invoke/begaininvoke to do this.But i dont understand how i can use invoke or begain invoke to add items in listview.Here is my code,

    Code:
    void PrepareTimers(List<int> _dataValues)
            {
                foreach (int dataValue in _dataValues)
                {
                    ThreadingTimer timer = new ThreadingTimer(new TimerCallback(TimerAction), dataValue, 0, 2000);
                }
    
            }
    
    void TimerAction(object flag)
    {
    
               flags_string = flag.ToString();
                string sql = "SELECT msisdn,sms FROM sms_data_bankasia where flag = " + flags_string + " LIMIT 1";
                MySqlCommand comd = mycon.CreateCommand();
                comd.CommandText = sql;
                MySqlDataReader dtr = comd.ExecuteReader();
                try
                {
                    if (mycon.State == ConnectionState.Open)
                    {
    
                        if (dtr.Read())
                        {
                            msisdn_string = dtr["msisdn"].ToString();
                            sms_string = dtr["sms"].ToString();
    
                        }
                        else
                        {
                            flag = 0;
                            msisdn_string = "NO MSISDN FOR " + (flags_string);
                            sms_string = "NO MSISDN FOR " + (flags_string);
                        }
                    }
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
    
                dtr.Close();
    
                string[] listitem ={ msisdn_string, sms_string, flags_string };
                ListViewItem li = new ListViewItem(listitem);
                
                lstResult.Items.Add(li);
    }
    Any one can help me to do this?Any help can be great appritiated.
     

Share This Page