2010-12-21 82 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實例。
但是,您不能在類型本身上調用它。