1

I understand in an async method that out and ref can not be used. But I am unclear on the consequences of using Action (or delegates). While I recognize that the value being set in the Action may not be available until after the await, are there any other problems with the below? Are their threading problems? I have googled extensively on this, but can't find clarity anywhere.

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 } 
2
  • 2
    There is nothing inherently wrong or dangerous about the code in the question. Commented Dec 6, 2018 at 20:52
  • 1
    Your code is fine. Commented Dec 6, 2018 at 21:44

1 Answer 1

2

When you always await a task, code from programmers point of view works really close to synchronous code. But when you start doing things like this

var task1 = FooAsync(setOutput); var task2 = BarAsync(setOutput); var result1 = await task1; var result2 = await task2; 

things start to get funny, because order they finish or threads they use is not guaranteed.

Anyway your code is fine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.