背景定时更新UI? [英] Background timer to update UI?
问题描述
我有一个小问题,我的应用程序。结果
我想更新每隔10秒我的UI的东西。我第一次使用了 DispatcherTimer
对于这一点,但它会阻止我的UI很短的时间,因为更新方法需要从web加载的东西,这个操作需要一定的时间。
现在我想到了一些后台工作的,我发现BackgroundTasks。结果
中的问题与后台任务是,据我理解正确的话,那他们应该作为更新程序,即使该应用程序将被暂停。我不需要这样。
我只是想,如果,如果它被挂起我的应用程序运行不更新。
I got a little problem with my application.
I would like to update something on my UI every 10 seconds. I first used a DispatcherTimer
for this but it will block my UI for a short time because the update method needs to load something from the web and this operation needs some time.
Now I thought about some kind of background worker and I found BackgroundTasks.
The problem with Background tasks is, as far as I understood it correctly, that they are supposed to serve as updaters even if the app is suspended. I don't need that.
I only would like to update if my app is running not if it is suspended.
有没有办法解决这个问题的好办法?
任何建议使用什么为这个提前?
Is there a good way to solve this? Any suggestions what to use for this?
谢谢!
推荐答案
您需要两样东西吧:
-
定时
Timer
您可以更新 System.Timers的UI。定时器
这10秒的时间间隔。
You can update the UI in System.Timers.Timer
with the 10 seconds interval.
调度
您需要使用 Dispatcher.Invoke
更改UI未持有的主UI线程的。相反,该方法过程
应该叫上的单独的线程的(定时
方法),其他比的主UI线程的,并使用调度
它提醒了变主UI线程。
You need to use Dispatcher.Invoke
to change the UI without holding the main UI thread. Instead the method Process
should be called on a separate thread (Timer
method), other than main UI thread, and use Dispatcher
in it to alert main UI thread for the change.
Process() // method to be called after regular interval in Timer
{
// lengthy process, i.e. data fetching and processing etc.
// here comes the UI update part
Dispatcher.Invoke((Action)delegate() { /* update UI */ });
}
这篇关于背景定时更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!