[英]Passing Action<> into Async Method
我知道在異步方法中,不能使用out和ref 。 但是我不清楚使用Action (或委托)的后果。 盡管我認識到“操作”中設置的值可能要等到等待之后才能使用,但以下內容還有其他問題嗎? 是他們的線程問題嗎? 我已經對此進行了廣泛的搜索,但是在任何地方都找不到清晰的地方。
protected async Task<gPeriod> MapPeriod(string value, Action<int> setOutput)
{
(...) //omitted code
int x = await MyMethodAsync(value)
setOutput(x);
return gPeriod; //calculation of this not shown in this example
}
當您始終await
任務時,從程序員的角度來看,代碼的工作確實接近於同步代碼。 但是當你開始做這樣的事情時
var task1 = FooAsync(setOutput);
var task2 = BarAsync(setOutput);
var result1 = await task1;
var result2 = await task2;
事情開始變得有趣,因為不能保證完成的順序或使用的線程。
無論如何,您的代碼都可以。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.