I am converting a VB Form code that uses InvokeRequired to C# WPF
// VB
If Me.InvokeRequired Then
MessageBox.Show("Apple")
Else
MessageBox.Show("Orange")
End If
The VB code returns “Apple“
// C#
if (this.Dispatcher.CheckAccess())
{
MessageBox.Show("Apple");
}
else
{
MessageBox.Show("Orange");
}
But my C# code return “Orange“
Isn’t C#’s CheckAccess same as VB Formc’sInvokeRequired? Why the boolean is inverted?
Dispatcher.CheckAccess()checks if you ARE associated with the thread, in our case its false.InvokeRequiredchecks if you NEED to be associated with the thread, in our case its true.You need to invoke (
InvokeRequired == true) when you’re not associated with the specific thread. In other words, logical definition of invoke required would be:Invoke is required if you do not have access to the specific thread