2010-12-21 48 views
1

为什么下面的代码(.NET-4,扩展方法)不会让我使用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 { })); 
} 

编辑

SLaks此言后

更新版本

public static void DoEvents(this Application a) 
    { 
     if (a != null) 
     { 
      a.Dispatcher.Invoke(
       System.Windows.Threading.DispatcherPriority.Background, 
       new Action(delegate { })); 
     } 
    } 
+2

你为什么不使用你的参数? – SLaks 2010-12-21 17:59:49

+0

你想完成什么? – mcabral 2010-12-21 18:09:18

回答

3

只能调用扩展方法的一个实例。

因此,您将能够编写Application.Current.DoEvents(),因为这是一个Application实例。
但是,您不能在类型本身上调用它。