2014-09-12 22 views
0
Task t = navigation.PushModalAsync(new AuthLoginPage("facebook")); 
      t.Start(); 
      t.Wait(); 

如何解決該異常:System.InvalidOperationException:該任務未處於啓動狀態。System.InvalidOperationException:任務未處於要啓動的有效狀態

+0

那麼,它是什麼狀態?你看過't'嗎?另外,一般來說,只有當您在沒有同步上下文的情況下運行時,您應該手動等待任務。 – NathanAldenSr 2014-09-12 14:57:40

+2

通常,從調用返回的任務返回.... Async()方法已經啓動。只要刪除通話't.Start()'。另外,請考慮使用'await'關鍵字。 – 2014-09-12 15:01:57

回答

0

你可以按照這個簡單的用法: -

// When you wanted to start the Async call. 
var t = navigation.PushModalAsync(new AuthLoginPage("facebook")); 
. 
. 
// do your other work. 
. 
. 
. 
// when you want the result of the Task t that you have started before. 
var result =await t; // task will wait here for getting result for you. 

您可以閱讀更多有關異步/等待。瞭解更多關於異步/等待的信息是非常好的,因爲它非常強大和簡單。看看Here for async/await

希望它可以幫助你。

0

通常當一個方法返回一個Task時,它已經啓動了,所以你不需要明確地啓動它。

如果您想等待其結果,請使用await =>await t;。如果您想阻止其執行,請調用Result屬性=>t.Result

相關問題