每2分鐘所以我創建用戶控件用的dataGridView,我實際上將如何對象是次要的,但讓我們說我有alredy數據源,我想刷新的dataGridView這些值。如何調用函數的用戶控件在.NET 3.5
的例子,我有功能fillDataGridView(),我想每2分鐘
我認爲我可以用Thread類中做到這一點,但它叫W/O任何成功還
如何你處理UI刷新?
我知道這看起來像「但與UI更新問題另一個人」,但是從我所看到的我真的不是最簡單的辦法做到這一點
public partial class Alertbox : UserControl
{
private static System.Timers.Timer aTimer;
public Alertbox()
{
InitializeComponent();
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(Update);
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
public void Update(object source, ElapsedEventArgs e)
{
BT_AddTrigger.Text += "test"; // append to button text
}
}
它喊這麼
System.Windows.Forms.dll中發生類型'System.InvalidOperationException'異常,但未在用戶代碼中處理其他 信息:跨線程操作無效:控制'BT_AddTrigger' 從非線程訪問它創建的線程。
爲了防止錯扣,使用:'BT_AddTrigger.Invoke(新行動(UpdateButtonText),新的對象[] {文本});'喜歡這裏:HTTPS:/ /gist.github.com/PopovMP/8f747dbd6948eccc69ad。但是,更好的選擇是使用'System.Windows.Forms.Timer',因爲你在UserControl中。 –