在C#中,我们知道有多个后台线程...所以我们可以创建多个UI线程吗?
并且多个UI线程可以帮助更新观察集合中的数据,而不需要freeez?
如果不是从webservice获取数据并更新到观察集合的最佳方法是什么?
代码:-
Thread lthrThread = new Thread((ThreadStart)delegate
{
string Data = DataFromServer()
this.Dispatcher.BeginInvoke(new Action(() =>
{
UI freeze here for 5 -10 seconds
}));
});
lthrThread.SetApartmentState(ApartmentState.STA);
lthrThread.Start();发布于 2017-04-24 19:35:23
UI线程是主thread.You不能复制it.If你在UI线程上调用你的数据,你的应用程序用户界面不会响应,直到它ends.So,你需要创建后台线程或任务来处理来自webservices.When的数据调用你从webservice获取数据并将其传递到主线程( UI )。您可以使用Dispatcher。
Application.Current.Dispatcher?.BeginInvoke(new Action(() => {
// Pass data to UI here.
}));或者你可以使用async Task,它会更方便。
https://stackoverflow.com/questions/43586816
复制相似问题