在wpf应用程序中显示busyindicator [英] Display busyindicator in wpf application
问题描述
我有一个来自wpf扩展工具包的BusyIndicator,并且我正在运行一个需要一段时间才能完成的函数.如果我在单独的线程中运行耗时的任务,则会收到NotSupportedException,因为我正尝试从该不同的线程将对象插入到ObservableCollection中.我真的不想花很多时间重构代码,如果可能的话...有没有办法我可以在一个单独的线程中设置指标的可见性呢?
I have a BusyIndicator from the wpf extended toolkit and I'm running a function that takes a while to complete. If I run the time consuming task in a separate thread, I get a NotSupportedException because I'm attemping to insert objects into an ObservableCollection from that different thread. I don't really want to spend a lot of time refactoring the code, if possible... Is there a way that I can set the visibility of the indicator in a separate thread instead?
编辑
ThreadStart start = delegate()
{
System.Windows.Application.Current.Dispatcher.Invoke((Action)(() =>
{
IsBusy = true;
}));
};
new Thread(start).Start();
longRunningFunction();
这对我也不起作用.
推荐答案
You should be able to use the Dispatcher
for things like that. e.g.
Application.Current.Dispatcher.Invoke((Action)(() =>
{
_indicator.Visibility = Visibility.Visible;
}));
这将导致代码在UI线程上运行.
This will cause the code to be run on the UI-Thread.
使用线程模型参考.
这篇关于在wpf应用程序中显示busyindicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!