I have a textbox on a window that reports the status and or success of a action. It’s bound to a property on the ViewModel.
So when a user activates the action that could take some time I wan’t to let him know that the action has started in the textBox. Problem is that he doesn’t write into the textbox until after the action has finished.
I guess it’s because the thread doesn’t give the UI the chance to do it’s work but not sure.
public class ViewModel : INotifyPropertyChanged
{
private string _report;
public string Report
{
get { return _report; }
set
{
_report = value;
RaisePropertyChanged("Report");
}
}
public void DoHeavyAction()
{
Report += "Heavy action readying";
ReadyHeavyAction();
Report += "Heavy action starting";
var result = DoTheHeavyAction();
if(!result.success)
{
report += "Heavy action failed";
return;
}
Report += result.value;
}
}
In this case the program lags for 2 seconds and then the whole thing appears:
Heavy action readying
Heavy action starting
Heavy action failed
Instead of appearing one by one.
Can I somehow use the dispatcher to update the View (note I’m using MVVM).
Yes, it’s easy. Your heavy operation runs in a separate thread, and you report the progress as
Edit:
In the actual case the operation is activated by an
ICommand. The saidICommandis dispatched from the UI, therefore it comes in the UI thread. So the problem is that the UI (and its update) is blocked by the heavy operation.The solution would be to start a new thread on the command arrival, and do the work there. The UI can be updated using the code like the example above.
There are some alternative approaches to this solution, but all of them are anyway starting the task in the background thread, although implicitly. A popular and good one is using a
BackgroundWorkerand updating the state in itsProgressChangedevent (note that this event comes in the UI thread, so you don’t need to use the Dispatcher in this case).