【发布时间】:2019-07-18 13:05:02
【问题描述】:
我想每 5000 毫秒更新一次表单中标签的文本,我一直在尝试使用计时器,但它不起作用,我不知道为什么。这是我正在使用的代码:
private void Form1_Load(object sender, EventArgs e)
{
openRequests();
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 20000;
aTimer.Enabled = true;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
codeComboBox.Items.Clear();
try
{
connectionDB.Open();
String query = "";
query = "SELECT GMKEY0 FROM SAM_FILNAS.EGESM1F0 WHERE GMSTX0 = '0' OR GMSTX0 = 'P' ";
daMAT = new OleDbDataAdapter(query, connectionDB);
dsMAT = new System.Data.DataSet();
daMAT.Fill(dsMAT, "sam_filnas.EGESM1F0 ");
foreach (System.Data.DataTable t in dsMAT.Tables)
{
foreach (System.Data.DataRow r in t.Rows)
{
codeComboBox.Items.Add(r["GMKEY0"]);
}
}
}
catch (Exception exc)
{
MessageBox.Show("non riesco a scaricare le richieste di manutenzione aperte");
}
label7.Text = "CI SONO " + codeComboBox.Items.Count.ToString() + " RICHIESTE DI MANUTENZIONE";
connectionDB.Close();
//updateTimer.Start();
resertAllTheControls();
}
计时器启动,每 5 秒调用一次 onTimeEvent 方法,但由于某种原因,该方法的执行似乎在 codeComboBox.Items.Clear(); 处停止
请解释一下为什么会这样。
【问题讨论】:
-
删除
codeComboBox.Items.Clear();是否有效?
标签: c# .net winforms timer controls