0
我是使用3層架構的新手。我有一個DataGridView的表單,我有一個有定時器的邏輯類。表單正在運行邏輯類的一個實例。計時器每10秒鐘處理一次。我被告知我應該使用事件,但我不明白這是如何工作的。現在,計時器運行,並且每十秒鐘運行onUpdated方法。下面是從邏輯類定時器:從邏輯層中的類更新UI層中的表單?
public void timerTick(object sender, EventArgs e)
{
t.Interval = 1000;
t.Enabled = true;
if (counter >= 10)
{
counter = 0;
OnUpdated();
}
else
{
counter++;
}
}
然後運行OnUpdated:
protected void OnUpdated()
{
if (Updated != null) Updated();
}
我想邏輯層運行的形式此方法。此方法獲取數據庫中的當前表。
private void dataGridViewUpdate()
{
dsCitizen.Clear();
dsTemp.Clear();
dataGridView1.DataSource = null;
dataGridView2.DataSource = null;
dsCitizen = lInst.getDataSetCitizen();
dsTemp = lInst.getDataSetTemporary();
dataGridView1.DataSource = dsCitizen.Tables[0];
dataGridView2.DataSource = dsTemp.Tables[0];
}
我敢肯定,我應該可以運行這個地方
lInst.Updated += Update; //lInst is an instance of my logic class in the form
但我不知道在哪裏。 如果您有其他解決方案,而不是使用活動,請成爲我的嘉賓! 在此先感謝。
只要確保在引用@ AbZy的答案時使用'System.Windows.Forms.Timer'。 – Colin