T
T
Tsiren Naimanov2015-10-17 18:19:55
Programming
Tsiren Naimanov, 2015-10-17 18:19:55

What's the difference in async methods?

string ret = await FileIO.ReadTextAsync(file);
and

var ret = FileIO.ReadTextAsync(file);
string str = ret.Result;

do not execute ...
but what's the difference?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Makarov, 2015-10-17
@ImmortalCAT

The difference is that await can only be used in an async method, and using it means that the compiler will turn the method into a coroutine, and in the place where await is located, its execution can be paused and switched to something else.
Using Result will stop your thread until the task completes and returns the result. But Result can also be used in an ordinary method, not only in async (in an async method, it is absolutely pointless to use it).
In addition, the use of Result entails non-obvious effects in the form of the possibility of deadlocks and complicates exception handling (because Result always throws an AggreagteException, which wraps an exception thrown inside a task), see more details here stackoverflow.com/questions/24623120/await -on-a-co...
I don't understand how the first answer answers your question)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question