开发者

Extension method question

Why the code bellow (.NET-4, Extension method) does not leave me using Applicati开发者_如何学运维on.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 { }));
        }
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜