yisic

在WINForm开发过程中,我们经常遇到填充比较多的数据到界面时,有时候界面卡死啦,这时候我们最好的办法是采用线程来对数据进行收集,然后再体现在界面上。

1.第一种是比较繁琐的采用异步进行操作。

创建一个委托: private delegate List<string> UpdateUIDelegate(int count);

制定委托方法: UpdateUIDelegate ui = GetData;

        //收集数据的方法

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;
}

异步回调:

private void UpdateCompleted(IAsyncResult asyncResult)
{
if (asyncResult == null) return;
List<string> lstDevice = (asyncResult.AsyncState as UpdateUIDelegate).EndInvoke(asyncResult);
//获取UI控件线程进行数据填充
this.button1.BeginInvoke(new Action(() => {
this.button1.Text = lstDevice[lstDevice.Count - 1];
}));
}

 

开始调用:

UpdateUIDelegate ui = GetData;
ui.BeginInvoke(5, UpdateCompleted, ui);

2.另外一个方法就是直接用new Thead() ,但必须设置CheckForIllegalCrossThreadCalls=false;

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-02-19
  • 2021-05-28
  • 2021-12-07
  • 2021-08-10
猜你喜欢
  • 2021-08-23
  • 2022-12-23
  • 2022-02-15
  • 2021-07-28
  • 2022-01-20
  • 2021-12-10
  • 2021-09-03
相关资源
相似解决方案