I have a boolean property in my viewmodel that binds to a property in my view. When this property changes to true I would like to make a service call without blocking the UI thread, so my view can carry on working. So far I have:
private bool _isLoadingAnimationVisible = true;
public bool IsLoadingAnimationVisible
{
get { return _isLoadingAnimationVisible; }
set
{
_isLoadingAnimationVisible = value;
if (IsLoadingAnimationVisible)
{
Task t = new Task(() => { LoadStuff(SelectedSomething.Id, SelectedDate, false); });
t.Start();
}
RaisePropertyChanged("IsLoadingAnimationVisible");
}
}
I don't think the method LoadStuff is important but this task t is currently blocking my UI thread. I was wondering whether there was a way to execute thread t while keeping the UI thread free.
You can do
if (IsLoadingAnimationVisible)
{
new Thread(delegate() {
LoadStuff(SelectedSomething.Id, SelectedDate, false);
}).Start();
}
Make sure you are not updating UI from LoadStuff
.
Try:
await Task.Factory.StartNew<bool>(()=>{get the bool})
You need to await a task for it to be async. The factory is a clean way of doing this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.