在 C# 中使用计时器执行 2 个 MySql 查询
我如何使用ThreadingTimer
执行2个mysql查询
?我希望第一个计时器更新数据库中的数据,第二个计时器计算更新了多少数据,并在窗口形式的标签中显示。这是我的代码,
void PrepareTimers(List<int> _dataValues)
{
foreach (int dataValue in _dataValues)
{
timer = new ThreadingTimer (new TimerCallback(TimerAction), dataValue, 0, 2000);
timer1 = new ThreadingTimer(new TimerCallback(TimerAction1), dataValue, 0, 2000);
//Console.WriteLine("Timer " + dataValue + " created.");
}
}
void TimerAction(object flag)
{
//Console.WriteLine("Timer start "+ flag.ToString());
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
string u = "UPDATED";
mycon.Open();
//Console.WriteLine("Timer " + flag.ToString() + " : " + "UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + " LIMIT 1");
MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + " LIMIT 1", mycon);
MySqlParameter param = new MySqlParameter();
param.ParameterName = "@flag";
param.Value = flag;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
mycon.Close();
}
void TimerAction1(object flag)
{
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
string sql = "SELECT count(flag) FROM sms_data_bankasia where sendingstatus='UPDATED' group by flag";
MySqlCommand comd = mycon.CreateCommand();
comd.CommandText = sql;
mycon.Open();
MySqlDataReader dtr = comd.ExecuteReader();
try
{
while (dtr.Read())
{
dict[timer] = label;
dict[timer].Text = dtr[0].ToString() + " program Updated";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();
}
它提供错误,“跨线程操作无效”。
我应该做什么?有什么好的建议吗?
How can i execute 2 mysql query
using ThreadingTimer
?I want 1st timer update data in db and 2nd timer count how much data are updated and also show in label in windows form.Here is my code,
void PrepareTimers(List<int> _dataValues)
{
foreach (int dataValue in _dataValues)
{
timer = new ThreadingTimer (new TimerCallback(TimerAction), dataValue, 0, 2000);
timer1 = new ThreadingTimer(new TimerCallback(TimerAction1), dataValue, 0, 2000);
//Console.WriteLine("Timer " + dataValue + " created.");
}
}
void TimerAction(object flag)
{
//Console.WriteLine("Timer start "+ flag.ToString());
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
string u = "UPDATED";
mycon.Open();
//Console.WriteLine("Timer " + flag.ToString() + " : " + "UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + " LIMIT 1");
MySqlCommand cmd = new MySqlCommand("UPDATE sms_data_bankasia set flag= @flag * 2 , sendingstatus = '" + u + "' WHERE flag = " + flag.ToString() + " LIMIT 1", mycon);
MySqlParameter param = new MySqlParameter();
param.ParameterName = "@flag";
param.Value = flag;
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
mycon.Close();
}
void TimerAction1(object flag)
{
string myconstring = "SERVER=localhost;" + "DATABASE=alicosms;" + "UID=root;" + "PASSWORD=;";
MySqlConnection mycon = new MySqlConnection(myconstring);
string sql = "SELECT count(flag) FROM sms_data_bankasia where sendingstatus='UPDATED' group by flag";
MySqlCommand comd = mycon.CreateCommand();
comd.CommandText = sql;
mycon.Open();
MySqlDataReader dtr = comd.ExecuteReader();
try
{
while (dtr.Read())
{
dict[timer] = label;
dict[timer].Text = dtr[0].ToString() + " program Updated";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
mycon.Close();
}
It provide error,"cross thread operation not valid".
What should i do? Any good suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Windows 窗体中,您不能从 UI 线程以外的任何其他线程在 UI 中执行任何操作。
您的计时器正在单独的线程上运行您的代码。
您可以尝试改用 System.Windows.Forms.Timer,因为它会为您将事件处理封送回 UI 线程。
In Windows forms, you cannot do anything in the UI from any other thread than the UI-thread.
Your timer is running your code on a seperate thread.
You could try to use the
System.Windows.Forms.Timer
instead, as it will marshal the event handling back to the UI thread for you.在这里您可以阅读解决方案
只是为了测试它是否解决了您的问题尝试包装您的 MessageBox 调用代码如下:
这部分是根据内存编写的,因此可能会略有不同。
Here you can read about the solution
Just to test if it fixes your issue try wrapping your MessageBox call in code like this:
This is written partly from memory, so it might vary a little.