WinForm 界面异步更新数据(方式二)
在WINForm開發(fā)過程中,我們經(jīng)常遇到填充比較多的數(shù)據(jù)到界面時,有時候界面卡死啦,這時候我們最好的辦法是采用線程來對數(shù)據(jù)進行收集,然后再體現(xiàn)在界面上。
1.第一種是比較繁瑣的采用異步進行操作。
創(chuàng)建一個委托:?private delegate List<string> UpdateUIDelegate(int count);
制定委托方法:?UpdateUIDelegate ui = GetData;
//收集數(shù)據(jù)的方法
? ? ? ? ? ? ? ? ? ? ? private List<string> GetData(int count)
? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? List<string> lst = new List<string>();
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < count; i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst.Add("item"+i);
? ? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? ? ? ?return lst;
? ? ? ? ? ? ? ? ? ? ? ? }
異步回調(diào):
? ? ? ? ? ? ? ? ? ? ?private void UpdateCompleted(IAsyncResult asyncResult)
? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (asyncResult == null) return;
? ? ? ? ? ? ? ? ? ? ? ? ? ? List<string> lstDevice = (asyncResult.AsyncState as UpdateUIDelegate).EndInvoke(asyncResult);
? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取UI控件線程進行數(shù)據(jù)填充
? ? ? ? ? ? ? ? ? ? ? ? ? ? this.button1.BeginInvoke(new Action(() => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?this.button1.Text = lstDevice[lstDevice.Count - 1];
? ? ? ? ? ? ? ? ? ? ? ? ? ? }));
? ? ? ? ? ? ? ? ? ? ? }
?
開始調(diào)用:
? ? ? ? ? ? ? ? ? ? ?UpdateUIDelegate ui = GetData;
? ? ? ? ? ? ? ? ? ? ?ui.BeginInvoke(5, UpdateCompleted, ui);
2.另外一個方法就是直接用new Thead() ,但必須設(shè)置CheckForIllegalCrossThreadCalls=false; ?/*不推薦使用,會有很多問題*/
轉(zhuǎn)自:?http://www.cnblogs.com/yisic/articles/3025234.html
轉(zhuǎn)載于:https://www.cnblogs.com/blosaa/archive/2013/05/14/3078335.html
總結(jié)
以上是生活随笔為你收集整理的WinForm 界面异步更新数据(方式二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle自定义函数
- 下一篇: 动态规划整理