1

Why the code bellow (.NET-4, Extension method) does not leave me using Application.DoEvents();?

/// <summary>
/// Invokes in the Dispatcher an empty action.
/// An thread safe equivalent of the ancient Application.DoEvents.
/// </summary>
public static void DoEvents(this Application a)
{
    Application.Current.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Background,
        new Action(delegate { })); 
}

EDIT

updated version after SLaks remark

    public static void DoEvents(this Application a)
    {
        if (a != null)
        {
            a.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Background,
                new Action(delegate { }));
        }
    }
serhio
  • 28,010
  • 62
  • 221
  • 374

1 Answers1

3

You can only call extension methods an an instance.

Thus, you would be able to write Application.Current.DoEvents(), since that's an Application instance.
However, you cannot invoke it on the type itself.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964