In regards to
Application.Current.Dispatcher.Invoke(action);
I have looked at CheckAccess()
and various ways of determining whether i'm on the main UI thread. Though after looking at the Dispatcher Source code for Invoke
, it seems to call CheckAccess()
and performs other checks anyway
Invoke Source Code
public void Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout)
{
...
...
// Fast-Path: if on the same thread, and invoking at Send priority,
// and the cancellation token is not already canceled, then just
// call the callback directly.
if (!cancellationToken.IsCancellationRequested && priority == DispatcherPriority.Send && CheckAccess())
{
SynchronizationContext oldSynchronizationContext = SynchronizationContext.Current;
try
{
DispatcherSynchronizationContext newSynchronizationContext;
if (BaseCompatibilityPreferences.GetReuseDispatcherSynchronizationContextInstance())
{
newSynchronizationContext = _defaultDispatcherSynchronizationContext;
}
else
{
if (BaseCompatibilityPreferences.GetFlowDispatcherSynchronizationContextPriority())
{
newSynchronizationContext = new DispatcherSynchronizationContext(this, priority);
}
else
{
newSynchronizationContext = new DispatcherSynchronizationContext(this, DispatcherPriority.Normal);
}
}
SynchronizationContext.SetSynchronizationContext(newSynchronizationContext);
callback();
...
So the most reliable way to check whether my dialog needs to be invoked is by calling Invoke
? looking at CheckAccess
and SynchronisationContexts
solutions when i dont have access to a Control
seem to be redundant.
Is this the case or is there some edge cases i'm missing, or hidden performance hit i cant see?