Friday, 15 February 2019

c# - Is it possible to call an awaitable method in a non async method?

You really shouldn't try to do something like that if you're on the UI thread, because that will mean you will block the thread. You should instead work around the ref parameter, for example by accepting a parameter of a simple class type, that contains the value you want to change.


Another reason why not to do this is that it still won't let you use ref parameters, because lambdas can't access ref parameters of the enclosing method.


But if you really want to do this (again, I really think you shouldn't), then you will need to get the result of the Task. Something like:


public string MyCallingMethod()
{
var task = Task.Run(async () =>
{
return await myMethodAsync();
});
return task.Result;
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...