2016-10-03 136 views
0

在我的基於MVVM的Wix Managed Bootstrapper應用程序中,在處理不同的事件時,我試圖向用戶顯示一個視圖來獲取一些輸入。它看起來像刻錄事件異步執行,因爲使用Dispatcher.Invoke(),它正在跳過或傳遞的視圖,並擊中最後一個事件,即不等待此輸入​​任務完成。
WPF:如何調用Dispatcher.Invoke()同步?

這裏是事件處理程序這就需要打最後一個之前完成:

請注意,當MessageBox.Show被彈出,它會等待,直到我們將其關閉。在調試時,我發現它實際上切換到MissingSourceView並加載了MissingSourceViewModel,但是在跳過它並執行ApplyComplete();

BootstrapperApplication.ResolveSource += (sender, e) => { 
         System.Windows.Forms.MessageBox.Show("Inside ResolveSource"); 

        WixBootstrapperData.CurrentDispatcher.Invoke(((Action)(() => 
         { 
          WixBootstrapperData.CurrentViewModel = new MissingSourceViewModel(WixBootstrapperData, InfoMessage); 
         }))); 
        }; 


BootstrapperApplication.ApplyComplete += (sender, e) => 
      {    
       WixBootstrapperData.BootstrapperApplicationModel.FinalResult = e.Status; 
       WixBootstrapperData.CurrentDispatcher.Invoke((Action)(() => 
       { 
        InfoMessage = AppResource.MsgFinished; 
        WixBootstrapperData.CurrentViewModel = new FinishViewModel(WixBootstrapperData, InfoMessage); 
       } 
       )); 
      }; 

我想,我應該用等待與ResolveSource()異步,但我面對類似的錯誤:

錯誤CS1061 'BaseViewModel' 不包含一個定義 'GetAwaiter' 和沒有擴展方法「GetAwaiter」接受類型「任務」的第一 參數可以找到(是否缺少使用 指令或程序集引用?)

任何想法如何使它在ResolveSource()內部等待完成,然後跳轉到它想要的位置?

+0

你可以選擇手動同步機制,如復位事件:https://msdn.microsoft.com/en-us/library/system.threading.manualresetevent%28v=vs.110%29.aspx ?f = 255&MSPPError = -2147217396 – sondergard

回答

0

使用此,請告訴是否這解決了您的問題。

 WixBootstrapperData.CurrentDispatcher.Invoke(((Action)(() => 
     { 
      Task.Factory.StartNew(() => { 
       WixBootstrapperData.CurrentViewModel = new MissingSourceViewModel(WixBootstrapperData, InfoMessage); 
      }).RunSynchronously(); 
     }))); 
+0

發生以下異常時崩潰: System.InvalidOperationException:RunSynchronously可能不會在已啓動的任務上調用。 –

+0

CurrentDispatcher是運行調度和調用啓動另一個運行在相同的調度線程.. –

+0

@FarrukhWaheed看到更新的答案 – AnjumSKhan