我正在學習異步並等待在C#中的操作。當它處理多個異步操作時,我無法理解執行流程。例如:我有我的c#應用程序中的下面的代碼。瞭解Aync並在C中等待#
await repository.GetAsync(values);//execute for 10 sec
var result = repository.setAsync(data); //20 sec
dataresult = await repository.GetAsync(result);//execute for 10 sec
我在這裏有三個異步調用。
根據我的理解,每個通話都會有一個回調,這不會等待一個動作完成。
那麼我如何確保行動完成?
repository.setAsync將在repository.GetAsync(values)完成其執行之前執行?或者這隻會在repository.GetAsync(values)執行完成後才執行?
那麼執行的順序是什麼?
1)
await repository.GetAsync(values);//started await method execution,since there is no callback it will not set and will start execute the next before complete this.
var result = repository.setAsync(data); //will execute for 20 sec. Once completed will go to previous thread and complete that.
2)
await repository.GetAsync(values);//started await method execution,complete it and move to the next line.
var result = repository.setAsync(data); //will execute for 20 sec.
當您執行同步的東西,你等待它在移動到另一個任務之前完成。當你異步執行某些東西時,你可以在完成之前移動到另一個任務。但是在這裏,爲了異步它等待操作完成。爲什麼這個矛盾?
我只想在操作完成後返回數據收集。 我覺得這是違反消防而忘了。這兩者是相同還是不同的概念? 按以下連結reference
該關鍵字的await不會阻塞線程直到任務完成 。
但從這裏發佈的答案,我明白這將暫停執行。這是真的嗎?我錯過了什麼嗎?
簡單地說:'await'是一個「異步等待」,所以*方法*將等待它完成,但*線程*未被阻止。您可能會發現我的[異步介紹](http://blog.stephencleary.com/2012/02/async-and-await.html)很有幫助。 –