Is there an equivalent to Form.InvokeRequired in WPF, e.g. Dispatcher.InvokeRequired?
This is slightly odd as it doesn't appear in intellisense, but you can use:
var dispatcher = myDispatcherObject.Dispatcher;
if (dispatcher.CheckAccess()) { /* ... */ }
As all UI components inherit from DispatcherObject
this should solve your specific problem, but it is not specific to the UI thread - it can be used for any dispatcher.
The equivalent is Dispatcher.CheckAccess.
A possible solution that came to mind is:
if ( Dispatcher.Thread.Equals( Thread.CurrentThread ) )
{
Action( );
}
else
{
Dispatcher.Invoke( Action );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With