jeudi 17 septembre 2015

Wrap a synchronous method in Asynchronous call c#

Consider the following code:

public int AddValues(int a, int b)
{
  // some really boring or extremely long DRY method code 
  return a + b;
}

Adding an Asynchronous Calling Method - Could I or Should I do this: saves time writing code ; Ensures the functionality of the inner code is the same as Synchronous code.

public async task AddValuesAsync(int a, int b)
{
  return await Task.FromResult(AddValues(a,b));
}

And here is my other question and maybe an answer to me; if Async method is called anytime the sync method is called I get an oopse (object oriented programming stupid error).

So what should I do in this situation - how to keep it DRY and keep it simple? I could make the sync method private and force all calls to the method be Async .. If you say my example code is a bad way to do it, please give a short reason why and a simple example of a good way to do it ..and any suggestions on what is best and how to do this ?

I have looked here Is wrapping a synchronous call in a Task.Run() to make it asynchronous beneficial? but that does not seem to answer very clearly , what I would like to know.

Aucun commentaire:

Enregistrer un commentaire