简体   繁体   English

C#WPF更新UI应用程序冻结

[英]C# WPF Update UI Application freeze

In C# we know there is multiple Background thread ... so like that can we create multiple UI thread? 在C#中,我们知道有多个背景线程...这样我们可以创建多个UI线程吗?

and Multiple UI thread helps to update data in observation collection without freeez? 和多个UI线程可帮助您在没有freeez的情况下更新观察数据集中的数据?

if No. what is the best way to get data from webservice and update to observation collection.. 如果不是,从Web服务获取数据并更新到观察集合的最佳方法是什么。

Code:- 码:-

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();

UI thread is main thread.You cannot copy of it.If you call your data on ui thread,your application user interface will not respond until it ends.So,you need to create background thread or task that handles data call from webservices.When you get data from webservice pass it to the main thread (UI). UI线程是主线程。您无法复制它。如果在ui线程上调用数据,则应用程序用户界面直到它结束都不会响应。因此,您需要创建后台线程或任务来处理来自Web服务的数据调用。您从Web服务获取数据,并将其传递到主线程(UI)。 You can use Dispatcher for it. 您可以使用Dispatcher

Application.Current.Dispatcher?.BeginInvoke(new Action(() => {
 // Pass data to UI here.
}));

or you can use async Task which will be more convenient. 或者您可以使用async Task ,这将更加方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM