【发布时间】:2018-08-17 03:51:15
【问题描述】:
这是我想要实现的目标: 我正在 c# (visual studio) 中以 windows 窗体格式构建警报系统。我有一个带有数据网格视图的 Windows 窗体,它需要每 5 秒从 MySQL 数据库中获取信息。我正在使用 System.Threading.Timer 和后台工作人员。我为此搜索了所有线程,但似乎没有任何效果。计时器正在工作,因为我每 5 秒出现一次线程错误,所以问题一定出在后台工作代码中。代码如下:
public partial class Alerts : Form
{
private System.Threading.Timer _timerThread;
private int _period = 5000;
static BackgroundWorker bgw = new BackgroundWorker();
public Alerts()
{
InitializeComponent();
_timerThread = new System.Threading.Timer((o) =>
{
// Stop the timer;
_timerThread.Change(-1, -1);
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerAsync();
// start timer again (BeginTime, Interval)
_timerThread.Change(_period, _period);
}, null, 0, _period);
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=*******";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(" select * from shopmanager.alerts ;", conDataBase);
try
{
conDataBase.Open();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dataGridView1.DataSource = bsource;
sda.Update(dbdataset);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conDataBase.Close();
}
static void bgw_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
// I think this is where I should be able to update the datagridview but i'm not sure how.
}
}
我完全迷路了。请帮忙
我现在尝试了这段代码,但它无法识别 DataGridView1。我做错了什么?
public partial class Alerts : Form
{
private System.Threading.Timer _timerThread;
private int _period = 5000;
static BackgroundWorker bgw = new BackgroundWorker();
private static object dbdataset;
public Alerts()
{
InitializeComponent();
_timerThread = new System.Threading.Timer((o) =>
{
// Stop the timer;
_timerThread.Change(-1, -1);
// Calls UpdateAlerts() that updates a datagridview with the mysql data
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerAsync();
// start timer again (BeginTime, Interval)
_timerThread.Change(_period, _period);
}, null, 0, _period);
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=******";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(" select * from shopmanager.alerts ;", conDataBase);
try
{
conDataBase.Open();
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
sda.Update(dbdataset);
e.Result = dbdataset;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conDataBase.Close();
}
static void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataGridView1.DataSource = dbdataset;
}
}
【问题讨论】:
-
您无法从
DoWork事件处理程序更新表单上的对象;您必须从RunWorkerCompleted更新它们。DoWorkEventArgs e对象有一个e.Result属性,您可以在DoWork中分配它(例如,使用dbdataset),然后在RunWorkerCompleted中读取它。本教程可能会有所帮助:docs.microsoft.com/en-us/dotnet/framework/winforms/controls/… -
您应该查找 Invoke。并养成引用收到的错误消息的习惯!
-
感谢您的回复,但我已经阅读了。我想知道的是如何正确编码(对于我的特殊情况)。谢谢