Invoking UI Changes in WPF

By Charlotte

Again another reminder, in WinForms I would have done:

private delegate void UpdateUiTextDelegate(Control control, string text);
private void UpdateUiText(Control control, string text)
{
if(InvokeRequired)
{
Invoke(new UpdateUiTextDelegate(UpdateUiText), new object[] {control, text});
return;
}
control.Text = text;
}

Using the same delegate we need to use the Dispatcher.Invoke method – this is (as far as I’m aware at the moment) the sort-of equivalent of the Invoke method on a Form.


private void UpdateUiText(Control control, string text)
{
if(!Dispatcher.CheckAccess())
{
Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(UpdateUiText), control, text);
return;
}
control.Text = text;
}

To be honest, I think I’ve read somewhere that the Dispatcher isn’t equivalent – but it does the job for now… I need to read into it more!