2015-07-22 58 views
0

在我的WPF控件中,我通過以下方式實現了異步模式以執行非UI任務。在Windows窗體應用程序中承載WPF控件

internal static void Execute(Action action) 
    { 
     if (System.Windows.Application.Current != null) 
     { 
      if (System.Windows.Application.Current.Dispatcher.CheckAccess()) 
       action(); 
      else 
       System.Windows.Application.Current.Dispatcher.BeginInvoke(action, null).Wait(); 
     } 
    } 

這適用於WPF應用程序。當我在Windows窗體應用程序中藉助ElementHost使用我的WPF控件時,我無法使用上述方法,因爲System.Application.Current將爲空。

現在我必須知道以下幾點。

1)當控件託管在Windows窗體應用程序中時,是否可以訪問我的WPF控件中的UI線程? 2)如果可能,請親切指導我如何實現它。

+0

[BackgroundWorker Class](https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v = vs.110).aspx)正是爲此目的而設計的 – Fabio

回答

0

我找到了答案。只需要初始化一個應用程序實例即可。

if(Application.Current == null) new Application();

相關問題