c# Task.FromResult 的基本用法和實例


c#  Task.FromResult 的基本用法和實例

This method is useful when you perform an asynchronous operation that returns a Task object, and the result of that Task object is already computed

There are two common use cases I've found:

  1. When you're implementing an interface that allows asynchronous callers, but your implementation is synchronous.
  2. When you're stubbing/mocking asynchronous code for testing

上面總體來說是用於單元測試;

然后還有在我們讀取緩存的時候有用; 微軟官方博客實例;

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-create-pre-computed-tasks

上面的思想:

This method is useful when you perform an asynchronous operation that returns a Task object, and the result of that Task object is already computed.

已經計算出來了,或者已經緩存好了,基本想要的值;

 

這里添加一個基本的鏈接功能:

那就是我們的異步方法中的 進度報告的問題;https://social.technet.microsoft.com/wiki/contents/articles/19020.progress-of-a-task-in-c.aspx

效果是非常好的呀;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM