Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to InvokeRequired in WPF

Is there an equivalent to Form.InvokeRequired in WPF, e.g. Dispatcher.InvokeRequired?

like image 835
PVitt Avatar asked Oct 14 '11 08:10

PVitt


3 Answers

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.

like image 106
Steve Greatrex Avatar answered Nov 14 '22 04:11

Steve Greatrex


The equivalent is Dispatcher.CheckAccess.

like image 44
Nick Butler Avatar answered Nov 14 '22 05:11

Nick Butler


A possible solution that came to mind is:

if ( Dispatcher.Thread.Equals( Thread.CurrentThread ) )
{
    Action( );
}
else
{
    Dispatcher.Invoke( Action );
}
like image 3
PVitt Avatar answered Nov 14 '22 05:11

PVitt