create timer using list value and assign those timer to execute query in C#
I have a list and i want to use this list value to create timer.And also want to use those timer to execute 2 MySql query. Here is my code,
Timer timer;
List<uint> _dataValues = new List<uint>();
private void button1_Click(object sender, EventArgs e)
{
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
string sql = "SELECT flag FROM sms_data_bankasia group by flag";
MySqlCommand comd = mycon.CreateCommand();
comd.CommandText = sql;
mycon.Open();
MySqlDataReader dtr = comd.ExecuteReader();
count = 0;
while (dtr.Read())
{
_dataValues.Add(dtr.GetUInt32(0));
}
dtr.Close();
}
void PrepareTimers(List<uint> dataValues)
{
foreach (uint dataValue in _dataValues)
{
timer = new Timer(TimerAction, dataValue, 1000, 0);
}
}
void TimerAction(object flag)
{
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM sms_data_bankasia WHERE flag = @flag", mycon);
MySqlParameter param = new MySqlParameter();
param.ParameterName = "@flag";
param.Value = flag;
cmd.Parameters.Add(param);
}
In PrepareTimers section provide error.I also want to add another query in "TimerAction".S开发者_运维问答o what to do ?Any one can help me?I am using VS 2005.net and C# language.
Since your error syas that No overload for method 'Timer' takes '4' arguments
, I'm guessing that you are using System.Timers.Timer
class, for which there is no constructor which takes 4 arguments.
As per your code, you need to use:
System.Threading.Timer
Edit(as per the comment): In order to overcome from this naming conflict, either you can create an alias for the nemespace like
using ThreadingTimer = System.Threading.Timer;
and use ThreadingTimer
or use fully qualified name like:
System.Threading.Timer = new System.Threading.Timer(TimerAction, dataValue, 1000, 0);
精彩评论