Wednesday 26 September 2018

c# - How do I offer a synchronous version of my async method?

I have this async method in C# code:




public async Task DoYourThingAsync()
{
....
}


Now, for some legacy code I have to offer a synchronous version of this method, similar to how for example HttpClient offers synchronous and async versions of the same method. I do not want to copy-paste my code to a second method, so I did this:



public int DoYourThing()
{

return DoYourThingAsync().Result;
}


Question: is this the correct way to accomplish this?

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 &q...