As the title suggests, I find myself in this situation a fair amount and believe I'm doing something wrong. Frequently I want a UI that is constantly updating based on the work of background tasks, but find that the separation between simply trying to update the UI and doing work on the UI thread can be hard to find.
Say I have a simple DependencyObject:
public class TaskItem : DependencyObject
{
public string Name {get;set;}
public string Command {get;set;}
public WorkState State
{
get { return (WorkState)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(WorkState),
typeof(TaskItem));
}
These objects are stored in an ObservableCollection, called mTaskItems. On some user event, I want to do some work with these objects:
Task.Run(new Action( () =>
{
foreach (TaskItem task_item in mTaskItems.Where(n => n.State !=
WorkState.Executing))
{
DoSomeWork(task_item);
}
}));
I can't check the DependencyProperty State of the TaskItem in the above sample because it's now on a different thread from which owns it.
Is it normal to have dispatchers sprinkled throughout the business logic if you want to update the UI during processing or am I going about this in a completely wrong way?
Should my dependency objects not be used as business data containers at all, but just exist as UI data containers? (if so what's the best design to link them?)
Thanks.