Friday, 6 April 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...