Where is my Control.InvokeRequired?
‘Control.InvokeRequired’ is one of the mostly used functions in developing muti-threaded GUIs in .NET Framework. While developing a sample application for Vista, I wanted to use the same functionality but new WPF controls living in System.Windows.Controls namespace don’t have ‘InvokeRequired’ (similarly don’t have Invoke and BeginInvoke methods as WPF Controls do not implement ISynchronizeInvoke interface). So, what’s the solution? WPF introduces two new classes DispatcherObject and Dispatcher. They provide the functionality to manipulate your WPF controls from other threads safely. As an example, the following code snippet in .NET framework 1.0 – 2.0:
private void UpdateMyButton()
{
if (!this.button1.InvokeRequired)
{
//I’m on the GUI thread!
//Safe to update my ‘button1′ properties
button1.Text = “Updated”;
}
else
{
//Synchronize my access!
button1.BeginInvoke(new UpdateGUIDelegate(UpdateMyButton));
}
}
should be replaced with this one if you’re using WPF controls in .NET Framework 3.0:
private void UpdateMyWPFButton()
{
if (this.button1.CheckAccess())
{
this.button1.Content = “Updated!”;
}
else
{
this.button1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateDelegate(this.UpdateMyWPFButton));
}
}



October 24th, 2006 at 2:02 pm
Might also want to check out Karsten’s blog posting here highlighting a couple WPF methods that are hidden from intellisense.
January 6th, 2007 at 6:32 pm
Thanks much! I was just wondering what happened to that…
January 9th, 2007 at 4:29 pm
This information was exactly what I needed. It solved my coding problem. Here is a snippet…
if (!this.result.CheckAccess())
{
this.result.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new EventHandler (this.workflowRuntime_WorkflowCompleted), sender, e);
}
else
{
this.result.Text = e.OutputParameters["Result"].ToString();
// Clear fields
this.amount.Text = string.Empty;
// Disable buttons
this.approveButton.IsEnabled = false;
this.rejectButton.IsEnabled = false;
}