当在UI线程更新数据时,界面会卡顿3-10秒,我希望在不卡顿的情况下在UI线程中更新数据。
代码:
Task t = Task.Factory.StartNew(() =>
{
// Get data from Server
GetData(true);
});
在
Getdata()
函数内部//Converst JSON to DataSet Object:- "tempDataSet"
Task task = Task.Factory.StartNew(() =>
{
RetriveData(tempDataSet, firstTime);
}, CancellationToken.None, TaskCreationOptions.None, MainFrame.Current);
在
RetriveData
函数内部DataTable response = tempDataSet.Tables["response"];
DataTable conversations = tempDataSet.Tables["convo"];
foreach (DataRow row in conversations.Rows) // UI Hangs in the method
{
UC_InboxControl control = new UC_InboxControl(row, uC_Inbox);
if (uC_Inbox.mnuUnreadChat.IsChecked == false)
{
inboxControlCollection.Add(control);
}
else
{
inboxUnreadOnlyControlCollection.Add(control);
}
}
什么是在UI线程中更新UI的最佳方法,不会出现挂起或冻结的情况?